This
example source code (for the EZ-USB family of USB Microcontrollers) sets
up the Serial Port 0 Baud rates in various modes to produce a number of
different baud rates and modes.
#include <REG320.h>
#include <ezusb.h>
#include <ezregs.h>
WaitForTx( void )
{
unsigned int timeout;
timeout = 10000;
while ((timeout--) && (TI==0));
}
main()
{
// Setup the output pins for Tx/Rx on Serial Port 0
PORTCCFG |= 3; // Rx0 and Tx0 on PC.0 and PC.1
//*******************************************************
// Mode 0: Synchronous 2MHz
//*******************************************************
SM0 = 0; SM1 = 0; // Mode 0
SM2 = 0;
// In Mode 0, CLK24/12 (2MHz)
REN = 1;
// Receive Enabled
TI = 0;
// Clear the transmit flag
SBUF0 = 0xAA;
// Send the character
WaitForTx();
// Wait for the character to be sent
//*******************************************************
// Mode 0: Synchronous 6MHz
//*******************************************************
SM0 = 0; SM1 = 0; // Mode 0
SM2 = 1;
// In mode 0, CLK24/4 (6MHz)
REN = 1;
// Receive Enabled
TI = 0;
// Clear the transmit flag
SBUF0 = 0xAA; // Send the character
WaitForTx(); // Wait for the character to be sent
//*******************************************************
// Mode 1: Asynchronous, Timer 1 (mode 2) baud rate
generator (SMOD=0,T1M=1)
//*******************************************************
SM0 = 0; SM1 = 1; // Mode 1
SM2 = 1;
// RI on valid STOP only
REN = 1;
// Receive Enabled
// Timer 1 setup
TMOD = 0x20;
// 0 0 1 0 x x x x (Timer 1 mode 2 - 8 bit auto reload)
CKCON |= 0x10;
// Set the T1M bit (Uses CLK24/4 (6MHz) as ref)
PCON &= ~0x80;
// Div by 32 (SMOD bit) (makes ref=6MHz/32=187.5kHz)
// BR = 187.25kHz/(255-TH1)
TH1 = 0xFF;
// 187.5kHz
// TH1 = 0xFE;
// 93.75kHz
// ...
TR1 = 1;
// Start the Timer 1 running
TI = 0;
// Clear the transmit flag
SBUF0 = 0xAA; // Send the character
WaitForTx();
//*******************************************************
// Mode 1: Asynchronous, Timer 1 (mode 2) baud rate
generator (SMOD=1,T1M=1)
//*******************************************************
SM0 = 0; SM1 = 1; // Mode 1
SM2 = 1;
// RI on valid STOP only
REN = 1;
// Receive Enabled
// Timer 1 setup
TMOD = 0x20;
// 0 0 1 0 x x x x (Timer 1 mode 2 - 8 bit auto reload)
CKCON |= 0x10;
// Set the T1M bit (Uses CLK24/4 (6MHz) as ref)
PCON |= 0x80;
// Div by 16 (SMOD bit) (makes ref=6MHz/16=375kHz)
// BR = 375kHz/(255-TH1)
TH1 = 0xFF;
// 375kHz
// TH1 = 0xFE;
// 187.5kHz
// TH1 = 0xFA;
// 62.5kHz (57600)
// TH1 = 0xF6;
// 37.5kHz (38400)
// TH1 = 0xF3;
// 28.84kHz (28800)
// TH1 = 0xEC;
// 18.75kHz (19200)
// TH1 = 0xD9;
// 9.615kHz (9600)
// TH1 = 0xB2;
// 4.807kHz (4800)
// TH1 = 0x64;
// 2.404kHz (2400)
TR1 = 1;
// Start the Timer 1 running
TI = 0;
// Clear the transmit flag
SBUF0 = 0xAA; // Send the character
WaitForTx();
//*******************************************************
// Mode 1: Asynchronous, Timer 1 (mode 2) baud rate
generator (SMOD=0,T1M=0)
//*******************************************************
SM0 = 0; SM1 = 1; // Mode 1
SM2 = 1;
// RI on valid STOP only
REN = 1;
// Receive Enabled
// Timer 1 setup
TMOD = 0x20;
// 0 0 1 0 x x x x (Timer 1 mode 2 - 8 bit auto reload)
CKCON &= ~0x10;
// Set the T1M bit (Uses CLK24/12 (2MHz) as ref)
PCON &= ~0x80;
// Div by 32 (SMOD bit) (makes ref=2MHz/32=62.5kHz)
// BR = 62.5kHz/(255-TH1)
TH1 = 0xFF;
// 62.5kHz
// TH1 = 0xFE;
// 31.25kHz
// ...
TR1 = 1;
// Start the Timer 1 running
TI = 0;
// Clear the transmit flag
SBUF0 = 0xAA; // Send the character
WaitForTx();
//*******************************************************
// Mode 1: Asynchronous, Timer 1 (mode 2) baud rate
generator (SMOD=1,T1M=0)
//*******************************************************
SM0 = 0; SM1 = 1; // Mode 1
SM2 = 1;
// RI on valid STOP only
REN = 1;
// Receive Enabled
// Timer 1 setup
TMOD = 0x20;
// 0 0 1 0 x x x x (Timer 1 mode 2 - 8 bit auto reload)
CKCON &= ~0x10;
// Set the T1M bit (Uses CLK24/12 (2MHz) as ref)
PCON |= 0x80;
// Div by 16 (SMOD bit) (makes ref=2MHz/16=125kHz)
// BR = 125kHz/(255-TH1)
TH1 = 0xFF;
// 125kHz
// TH1 = 0xFE;
// 62.5kHz
// ...
TR1 = 1;
// Start the Timer 1 running
TI = 0;
// Clear the transmit flag
SBUF0 = 0xAA; // Send the character
WaitForTx();
//*******************************************************
// Mode 1: Asynchronous, Timer 2 (mode 2) baud rate
generator (SMOD=1,T2M=0)
//*******************************************************
SM0 = 0; SM1 = 1; // Mode 1
SM2 = 1;
// RI on valid STOP only
REN = 1;
// Receive Enabled
// Timer 2 setup
TCLK = 1;
// Use the Timer 2 for the Tx clk
RCLK = 1;
// Use the Timer 2 for the Rx clk
C_T2 = 0;
// Use it as a timer, not counter
TH2 = 0xFF;
// Set the counter to reload soon
TL2 = 0xFF;
// BR = 24MHz/(32x(65536 - RCAP2H,RCAP2L))
// RCAP2H = 0xFF; RCAP2L = 0xFF;
// 750kHz
// RCAP2H = 0xFF; RCAP2L = 0xFA;
// 115.2kHz (125kHz)
// RCAP2H = 0xFF; RCAP2L = 0xF3;
// 57.6kHz
// RCAP2H = 0xFF; RCAP2L = 0xEC;
// 38.4kHz
// RCAP2H = 0xFF; RCAP2L = 0xE6;
// 28.8kHz
RCAP2H = 0xFF; RCAP2L = 0xD9;
// 19.2kHz
// RCAP2H = 0xFF; RCAP2L = 0xB2;
// 9.6kHz
// RCAP2H = 0xFF; RCAP2L = 0x64;
// 4.8kHz
// RCAP2H = 0xFE; RCAP2L = 0xC8;
// 2.4kHz
TR2 = 1;
// Start the Timer 2 running
TI = 0;
// Clear the transmit flag
SBUF0 = 0xAA;
// Send the character
WaitForTx();
// Loop forever
while (1);
}
|