Getting Started

(lily) #1

Chapter 6: C Functions and Program Structures


if( s[i] == '\0' ) break; // quit on string terminator

// set Clock Prescaler Change Enable
// set prescaler = 4, Inter RC 8Mhz / 4 = 2Mhz

oid OSCCAL_calibration(void)

R1B = (1<<CS10); // start timer1 with no prescaling
TCCR2A = (1<<CS20); // start timer2 with no prescaling

//wait for TCN2UB and TCR2UB to be cleared


void sendString(char s[])
{
int i = 0;


while(i < 64) // don't get stuck if it is a bad string
{


sendChar(s[i++]);
}
}


void USARTinit()
{
// Increase the oscillator to 2 Mhz for the 19200 baudrate:
CLKPR = (1<<CLKPCE);


CLKPR = (1<<CLKPS1);


// Set the USART baudrate registers for 19200
UBRR0H = 0;//(unsigned char)(baudrate>>8);
UBRR0L = 12;//(unsigned char)baudrate;


// Enable 2x speed change
UCSR0A = (1<<U2X0);


// Enable receiver and transmitter
UCSR0B = (1<<RXEN0)|(1<<TXEN0)|(0<<RXCIE0)|(0<<UDRIE0);


// Set the USART to asynchronous at 8 bits no parity and 1 stop bit
UCSR0C = (0<<UMSEL0)|(0<<UPM00)|(0<<USBS0)|(3<<UCSZ00)|(0<<UCPOL0);
}


//Calibrate the internal OSCCAL byte, using the external
//32,768 kHz crystal as reference
v
{
unsigned char calibrate = 0;//FALSE;
int temp;
unsigned char tempL;


CLKPR = (1<<CLKPCE); // set Clock Prescaler Change Enable
// set prescaler = 8, Inter RC 8Mhz / 8 = 1Mhz
CLKPR = (1<<CLKPS1) | (1<<CLKPS0);


TIMSK2 = 0; //disable OCIE2A and TOIE2


ASSR = (1<<AS2); //select asynchronous operation of timer2 (32,768kHz)


OCR2A = 200; // set timer2 compare value


TIMSK0 = 0; // delete any interrupt sources


TCC

Free download pdf