/***********************************************************************/
/* */
/* FILE :uart.c */
/* DATE :Thu, May 27, 2008 */
/* DESCRIPTION :uart program file. */
/* CPU GROUP :M30290 */
/* by :liufeng */
/* */
/* */
/***********************************************************************/
#include "sfr_M16C29config.h"
void delay(void);
void clock_initial(void);
void uarttx_initial(void);
void uartrx_initial(void);
void port_initial(void);
void main(void)
{
clock_initial();
port_initial();
uarttx_initial();
//uartrx_initial();
asm("NOP");
asm("NOP");
}
void clock_initial(void) //时钟初始化配置
{
unsigned char i;
prc1 = 1;
pm20 = 0;
prc1 = 0;
prc0 = 1; //允许写时钟
prc2 = 1;
pacr=0x03; //80引脚版本
prc1 = 1; // Enable write to PM0,PM1,PM2,TB2SC,INVC0 and INVC1 registers
pm21 = 0; // System clock protective bit
cm10=0;cm05=0;
for(i=0; i<10; i++);
cm15=1; //High drive capacity selected(cm1 bit 5)
cm07=0;cm21=0;cm11=0; //Main clock selected(cm0 bit 7,cm1 bit1,cm2 bit 1)
cm16=0;cm17=0;cm06=1; //CPU clock divide by 8 selected(cm0 bit 6, cm1 bit 6 and 7)
pclk5=0;cm01=0;cm00=0; //CLKout pin function as normal I/O port P90(pclkr bit 5,cm0 bit 0 and 1)
prc0 = 1;
pclk1 = 1; //f1SIO set
prc0 = 0; //写时钟保护, 系统时钟8分频
}
void uarttx_initial(void) //串口发送
{
unsigned int trans_data; //发送REG
trans_data = 0x0011;
u0brg = 0x81;
ucon = 0x00;
u0c0 = 0x30;
u0c1 = 0x00;
u0mr = 0x05;
u0tb = trans_data;
u0c1 = 0x05;
trans_data = ucon;
trans_data = u0c0;
trans_data = u0c1;
trans_data = u0tb;
trans_data = u0c1;
/*
clk0_u0c0=0;
clk1_u0c0=0;
u0brg = 0x81;
u0mr = 0x05;
te_u0c1=0;
re_u0c1=0;
clk0_u0c0=0;
clk1_u0c0=0;
asm("NOP");
u0brg = 0x00; // Setting UART0 bit rate generator (Approx 9600bps @20MHz f1)必须在U0C0后
crd_u0c0 = 1; //禁止CTS RTS
ucon = 0x01;
u0tb = trans_data;
u0brg = 0x81;
te_u0c1=1;
re_u0c1=1;
u0brg = 0x81;
u1brg = 0x81;
ti_u0c1 = 0;
*/
while (1)
{
p9_0 = 1;
u0tb = trans_data;
while (ti_u1c1 == 0); //Wait for transmission buffer emty
u0tb = trans_data; // Set transmission data
te_u1c1 = 1; // Transmission enabled
p9_0 = 0;
trans_data++;
}
}
void uartrx_initial(void) //串口接收
{
unsigned int recevie_data; //接收REG
pd6_1 = 0;
pd6_2 = 0; //RXD direction bit
pd6_3 = 1; //TXD direction bit
u0mr = 0x05;
u0c0 = 0x00;
u0brg = 0x81; /* Setting UART0 bit rate generator (Approx 9600bps @20MHz f1)必须在U0C0后 */
ucon = 0x00;
u0c1 = 0x05;
while (1)
{
while (!ri_u0c1)
{
;
}
recevie_data = u0rb;
asm("NOP");
asm("NOP");
}
}
void delay(void) //p9_0 LED delay
{
unsigned int j,k;
j = 0;
k = 0;
for (;j<3000;j++);
for (;k<6000;k++);
}
void port_initial(void)
{
prc2 = 1; // Enable write to PD9,PACR and S4C registers
pd9_0 = 1; //P9 direction bit
pd6_1 = 0; // CLK direction bit
pd6_2 = 0; //RXD direction bit
pd6_3 = 1; //TXD direction bit
}