C language code for SCI for 68HC908AP64 (DEMO908AP64 board)

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

C language code for SCI for 68HC908AP64 (DEMO908AP64 board)

1,402 Views
surinder
Contributor I
Hi,

I want to write C program using SCI on DEMO908AP64.
I am not able to understand configuratin for calculating baud rate.
I read 68HC908AP64 datasheet and AN3035 (Using the HC08 SCI Module).
I read assembly code supplied with demo board.
I am more confused.

There is option of two xtal on the board.
I chose 32.768KHz

There are three clock sources for Oscillator module MUX.
1) external crystal(XCLK) 2) external RC (RCCLK) 3) internal clock (ICLK)
What is default value of ICLK?
Which one is selected by default? more specifically between ICLK and others.

The reference output of the OSC Module "CGMRCLK", goes through PLL and freq is multiplied K number of times.
What is the PLL frequency multiplier value by default ?

In CGM, there is option of selecting between these two.
CGMXCLK (coming directly from OSC module) OR CGMPCLK (multipled freq out from PLL)
Either of these divided by 2 to get CGMOUT.
What is the default selection?

And, Bus clock = CGMOUT ÷ 2

Is it true that Bus Clock will be always (CGMXCLK or CGMPCLK)/4. Any exceptions?
Is CGMXCLK frequency equal to XCLK in case external crystal is selected by OSC Module?

Now the baud selection part :smileyhappy:

From application note AN3035
------------------------------------------------
1. Configure the SCI clock source
CONFIG2 = 0x01; /* Internal data bus clock source used as clock source for SCI */
.
.
4.
/*****************************************************************
* Fbus = XTAL/4 2.4576 MHz *
* Baud Rate = -------------------- = ---------- = 9600 bps *
* 64 x SCP1:0 x SCR2:0 64 x 1 x 4
SCBR = 0x02; *
------------------------------------------------

What is meant by internal data bus clock? ICLK or Bus clock?
I guess it is Bus clock. In the scenario taken by this application note,
it appears that external crystal of 9.8304 MHz is used.

I want 9600 bps baud, 8 bit data, no parity, 1 stop bit
I need C code.
To use 32.768KHz external crystal, PLL is required to increase the frequency so that it can be divided (by atleast 64 * 4) to get 9600 with some accuracy.

Is there any program somewhere or someone can suggest the relevant configuration
register values?

Also, How can I get Bus clock frequency programmaticaly in C?

Regards
- Surinder
Labels (1)
0 Kudos
Reply
1 Reply

456 Views
surinder
Contributor I
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
0 Kudos
Reply