Hi,
After spending more time reading mcu datasheet, I could write following C code to send some strings to computer via serial port.
I used 9.8304Mhz crystal though.
There was some statement on the demo board manual that this crystal is for debug and I
took that too literaly :smileyhappy: I will try with 32.xxxx KHz crystal also.
there are way too many clock frequency calculations :smileysad:
-------------
void usart_init(void) {
CONFIG2 = 0x01; /* Internal data bus clock source used as clock source for SCI */
// 2. Configure the microcontroller’s pins for SCI communications
// PTB[2] as TxD, PTB[3]RxD [x,x,x,x,RxD,TxD,x,x]
DDRB &= ~(0x08); /* Configure Rx pin as input for reception */
PTB |= 0x04; /* Set Tx pin to have an idle state */
DDRB |= 0x04; /* Configure Tx pin as output for transmission */
// 3. Configure SCI control register 1, 2, and 3
SCC1 = 0x00; /* Loop mode disabled, disable SCI, Tx output not inverted,
8-bit characters, idle line wakeup, disable parity bit */
//SCC2 = 0x20; /* SCRIE SCI Receive Intr Enable, Disable transmitter and receiver */
SCC2 = 0x00; // disable receive intr
SCC3 = 0x00; /* Disable all error interrupts */
// SCI Baud Rate Register SCBR[x,x,SCP1,SCP0,x,SCR2,SCR1,SCR0]
//
// PCTL[BCS] ; 0 = CGMXCLK divided by two drives CGMOUT [default after reset]
// 1 = CGMPCLK divided by two drives CGMOUT
// SIM Bus Clock = CGMOUT/2
// when BCS == 0; CGMOUT = CGMXCLK/2
// ==> fBus = Bus clock = CGMXCLK/4 [default after reset]
// ==> fBus = XTAL/4
// If selecting external crystal of *** 9.8304 MHz ***
// SCP1,SCP0 = 0,0 => PD = 1 ; SCR2,SCR1,SCR0 = 0,1,0; BD = 4
// SCBR = [0 0 SCP1 SCP0 R SCR2 SCR1 SCR0] = 00000010 = 0x02
//
// SCI clock source
// baud rate = ----------------
// 64 × PD × BD
//
// SCI clock source = fBUS or CGMXCLK
// (selected by SCIBDSRC bit in CONFIG2 register)
// CONFIG2[SCIBDSRC],bit0 = 0 default after reset,
// 1 = Internal data bus clock, fBUS, is used as clock source for SCI
// 0 = Oscillator clock, CGMXCLK, is used as clock source for SCI
// .To use fBUS as clock source
// CONFIG2 = 0x01
// baud rate = fBUS/(64 * PD * BD)
// = (XTAL/4) / ( 64 * 1 * 4)
// = (9.8304/4) / ( 64 * 1 * 4)
// = 9600
SCBR = 0x02;
// 5. Enable receiver, transmitter, and SCI module
SCC1 |= 0x40; /* Enable SCI Module */
SCC2 |= 0x0C; /* Enable Transmitter and Receiver */
}
void usart_write_char(char c) {
while ((SCS1 & 0x80) == 0); /* Wait for the transmitter to be empty */
SCDR = c; // write data to data register
delay(1);
}
------------
- Surinder