本帖最后由 cnjxlxp 于 2012-4-15 22:34 编辑
#include <stdio.h>
#include "NUC1xx.h"
#include "DrvGPIO.h"
#include "DrvUART.h"
#include "DrvSYS.h"
uint8_t writechar1[1] ={0x55};//default write char
uint8_t getchar1;
//uart set//n,8,1,9600
void uart_init(void)
{
STR_UART_T param;
DrvSYS_SelectIPClockSource(E_SYS_UART_CLKSRC, 0);
DrvGPIO_InitFunction(E_FUNC_UART0);
param.u32BaudRate = 9600;
param.u8cDataBits = DRVUART_DATABITS_8;
param.u8cStopBits = DRVUART_STOPBITS_1;
param.u8cParity = DRVUART_PARITY_NONE;
param.u8cRxTriggerLevel = DRVUART_FIFO_1BYTES;
param.u8TimeOut = 0;
DrvUART_Open(UART_PORT0, ¶m);
}
void Sys_Init()
{
UNLOCKREG();
SYSCLK->WRCON.XTL12M_EN = 1;//12M
DrvSYS_Delay(5000);
LOCKREG();
uart_init();
DrvGPIO_Open(E_GPB, 10, E_IO_OUTPUT);//GPB10 output
DrvGPIO_ClrBit(E_GPB, 10);//if not clear GPB10, the buzzer will on. the default output is 0xffff;
DrvGPIO_Open(E_GPA,2, E_IO_OUTPUT); //GPA2 output, LED1
DrvGPIO_Open(E_GPA,3, E_IO_OUTPUT); //GPA3 output, LED2
}
void delay_ms(void)//delay form xiaobing's source code
{
uint32_t i,j;
for(i=1000;i>0;i--)
for(j=2395;j>0;j--);
}
int main (void)
{
Sys_Init();
while(1)
{
printf("hello world \n");//use printf to sent char from uart
DrvUART_Write(UART_PORT0,writechar1,1); //driver write uart
DrvSYS_Delay(5000);
printf("\n");
printf("input a char\n");//tell user to input a char
DrvSYS_Delay(5000);
while(UART0->ISR.RDA_IF==0);//stop here, till a char is received from uart to fifo
DrvUART_Read(UART_PORT0,writechar1,1);//driver read uart
DrvSYS_Delay(5000);
DrvUART_Write(UART_PORT0,writechar1,1);//driver write uart
DrvSYS_Delay(5000);
printf("\n");
printf("input a char\n");
DrvSYS_Delay(5000);//stop here, till a char is received from uart
getchar1=getchar();//use getchar to receive char from uart
DrvSYS_Delay(5000);
printf("the char you input is %c\n",getchar1);
DrvSYS_Delay(5000);
// scanf("%c\n",&getchar1);
getchar1=getchar();//use getchar to receive char from uart
DrvSYS_Delay(5000);
printf("the char you input is ");
putchar(getchar1);
DrvSYS_Delay(5000);
putchar('\n');
putchar('b');//use putchar to send char to uart
putchar('y');
putchar('e');
putchar('\n');
while(1)//led2 blinks, and buzzer on and off alternate
{
DrvGPIO_ClrBit(E_GPA,2);//GPA2=0
DrvGPIO_ClrBit(E_GPB, 10); //GPB10=0
delay_ms();
GPIOA->DOUT ^= 0x8; //LED2 turns on and off
DrvGPIO_SetBit(E_GPA,2);//GPA2=1
DrvGPIO_SetBit(E_GPB, 10);//GPB10=1
delay_ms();
}
}
}
|