本帖最后由 hotpower 于 2012-9-22 12:51 编辑
uart连接P00,P01,GND
按一下按键,灯亮一下,再按灯灭。按一下按键mcu从串口向电脑发送一个0x55
#include "Mini51.h"
#include "DrvGPIO.h"
#include "DrvSYS.h"
#include "DrvUART.h"
void EINT0Callback(void);
void delay_ms(uint32_t count);
void clock_init(void);
void port_init(void);
void eint0_init(void);
void sys_init(void);
uint8_t count;
void delay_ms(uint32_t count)
{
uint32_t i,j;
for(i=count;i>0;i--)
for(j=2395;j>0;j--);
}
void uart_init()
{
STR_UART_T sParam; //P00,P01
// Set UART I/O
DrvGPIO_InitFunction(FUNC_UART);
//Select UART Clock Source From 12MHz
DrvSYS_SelectIPClockSource(UART_CLK_SET, 0);
// UART Setting
sParam.u32BaudRate = 9600;
sParam.u8cDataBits = DRVUART_DATABITS_8;
sParam.u8cStopBits = DRVUART_STOPBITS_1;
sParam.u8cParity = DRVUART_PARITY_NONE;
sParam.u8cRxTriggerLevel= DRVUART_FIFO_1BYTES;
// Set UART Configuration
DrvUART_Open(UART_PORT0, &sParam);
}
void clock_init(void)
{
UNLOCKREG();
DrvSYS_Open(XTL_CLK);// external 12M as clock
while (DrvSYS_GetChipClockSourceStatus(XTL_CLK) != 1); //use delay is ok too
LOCKREG();
}
void port_init(void)
{
DrvGPIO_Open(E_PORT3, E_PIN1, E_IO_OUTPUT);//LED0
DrvGPIO_Open(E_PORT3, E_PIN6, E_IO_OUTPUT);//LED1
DrvGPIO_Open(E_PORT5, E_PIN2, E_IO_OUTPUT);//LED2
DrvGPIO_Open(E_PORT2, E_PIN6, E_IO_OUTPUT);//LED3
}
void eint0_init(void)
{
DrvGPIO_InitFunction(FUNC_EXTINT0);
DrvGPIO_EnableEINT(E_EINT0_PIN, E_IO_FALLING, E_MODE_EDGE, EINT0Callback);
}
void EINT0Callback(void)
{
uint8_t bInChar[1] = {0x55};
DrvGPIO_DisableEINT (E_EINT0_PIN);//disable intp0
delay_ms(20); //chattering ignore
if (DrvGPIO_GetBit (E_PORT3, E_PIN2)== 0)////chattering ignore
{
count=!count;
DrvUART_Write(UART_PORT0,bInChar,1);
}
DrvGPIO_EnableEINT(E_EINT0_PIN, E_IO_FALLING, E_MODE_EDGE, EINT0Callback); //enable intp0 again
}
void sys_init(void)
{
clock_init();
port_init();
eint0_init();
uart_init();
}
int main (void)
{
sys_init();
while(1)
{
if(count)
DrvGPIO_ClrBit(E_PORT3, 1);
else
DrvGPIO_SetBit(E_PORT3, 1);
}
} |