U0LCR = 0x83; // DLAB = 1,可设置波特率 Fdiv = (Fpclk / 16 / UART_BPS; //设置波特率 U0DLM = Fdiv / 256; U0DLL = Fdiv % 256; U0LCR = 0x03; }
2、向串口发送字节数据
void UART0_SendByte (uint8 data { // data=18;
U0THR = data; //发送数据
while ((U0LSR & 0x40 == 0; // 等待数据发送完毕 }
3、向PC机发送显示字符。
void PC_DispChar (uint8 x, uint8 y, uint8 chr, uint8 color {
UART0_SendByte(0xff; // 起始字符
UART0_SendByte(x; //发送字符显示坐标(x,y) UART0_SendByte(y;
UART0_SendByte(chr; //发送显示字符 UART0_SendByte(color; }
4、向上位机发送字符串。
void ISendStr (uint8 x, uint8 y, uint8 color, char *str
{ while (1 {
if (*str == '\\0' break; // 结束字符
PC_DispChar(x++, y, *str++, color; //发送显示数据 if (x >= 80 { x = 0; y++; } } }
5、定时器0中断服务程序。 void __irq IRQ_Timer0(void {
pinsave=(IO0PIN&pinvalue; if(pinsave!=pinvalue {
while(delay++==2 { delay=0;
if(pinsave == (IO0PIN&pinvalue switch(pinsave {
case 0x003e0000: { bo=bo+1; if(bo>4 bo=1; } break;
case 0x003d0000: {canshu+=1; if(canshu>2 canshu=1; } break;
case 0x003b0000: {switch(canshu {case 1: {if(hight<26 hight=hight+1;} break; case 2: {if(fc<100 fc=fc+2;} break;} }
break;
case 0x00370000: {switch(canshu {case 1: {if(hight<26 hight=hight-1;} break; case 2: {if(fc<100 fc=fc-2;} break;} } break; } } }
T0IR=0x01;
VICVectAddr=0x00;}
6、定时器1捕获中断服务程序 void __irq IRQ_Timer1(void { switch(bo { case 1: sin_wave(;
break; case 2: trip_wave(; break; case 3: fang_wave(; break; default: break;} T1IR=0x01; VICVectAddr=0x00; }
7、主程序 int main (void { char str[30];
const double T = (2 * 3.1415926/1024; //把一个周期分成2048份 for(i=0;i<1024;i++ {
table[i] = 512*(1+sin(T*i; }
PINSEL0 = 0x00000000; // 设置管脚连接GPIO,管脚功能选折寄存器全部为0时选为GPIO功能 PINSEL1=(2<<18; PINSEL2=PINSEL2&(~0x08;