本帖最后由 hotpower 于 2012-9-13 22:40 编辑
主函数#include "includes.h"
uint8_t buffer0[5] = {0x68,0x65,0x6c,0x6c,0x6f}; //定义HELLO的数组,是ASCII值
uint8_t buffer1[10] = {0x73,0x65,0x61,0x72,0x6f,0x76,0x65,0x72,0x37,0x39}; //searover79
uint8_t buffer2[1] = {0};
/*
* Function: main
* Description: 先串口输出hello,searover79
再等待从发送缓冲区接受数据,接收后再发送出去
* Input Patameter: 无
* Ouput Patameter : 无
* Returns: 无
*/
int main (void)
{
Set_System();
while (1)
{
DrvUART_Write(UART_PORT0,buffer0,5);//写串口函数
delay_ms(3000);
DrvUART_Write(UART_PORT0,buffer1,10);
delay_ms(3000);
DrvUART_Read(UART_PORT0,buffer2,1);
delay_ms(100);
DrvUART_Write(UART_PORT0,buffer2,1);
delay_ms(1000);
}
}
includes.h
#ifndef INCLUDES_H_
#define INCLUDES_H_
#include <stdio.h>
#include "NUC1xx.h"
#include "config.h"
#include "DrvSYS.h"
#include "DrvGPIO.h"
#include "Drvuart.h"
#endif
config.c
#include "includes.h" //包含所需的头文件
/*
* Function: Set_System
* Description: 封装一些初始化模块
* Input Patameter: 无
* Ouput Patameter : 无
* Returns: 无
*/
void Set_System(void)
{
RCC_Config(); //配置系统时钟
GPIO_Config(); //配置GPIO
UART_Config(); //配置USART
}
/*
* Function: RCC_Configuration
* Description: 系统时钟源设置
* Input Parameter: none
* Ouput Patameter: none
* Returns: none
*/
void RCC_Config(void)
{
UNLOCKREG();
DrvSYS_SetOscCtrl(E_SYS_XTL12M, 1); //使用外部12M晶振
delay_ms(100); //等待外部晶振就绪
LOCKREG();
}
/*
* Function: GPIO_Configuration
* Description: GPIO配置
* Input Parameter: 无
* Output Parameter: 无
* Returs: 无
*/
void GPIO_Config(void)
{
DrvGPIO_Open( E_GPA, 5, E_IO_OUTPUT ); //指定E_GPA5为输出,控制LED4阴极
DrvGPIO_Open( E_GPB, 0, E_IO_INPUT ); //RX
DrvGPIO_Open( E_GPB, 1, E_IO_OUTPUT ); //TX
}
/*
* Function: UART_Configuration
* Description: 配置USART
* Input Parameter: none
* Output Parameters: none
* Returns: none
*/
void UART_Config()
{
STR_UART_T param;
DrvSYS_SelectIPClockSource(E_SYS_UART_CLKSRC, 0); //使能UART时钟 CLKSEL1寄存器参考NUC100_120_TRM_SC_2.1.PDF
DrvGPIO_InitFunction(E_FUNC_UART0); //复用功能引脚设置 drvgpio_initfunction()给多功能引脚指定功能
param.u32BaudRate = 9600; //波特率9600
param.u8cDataBits = DRVUART_DATABITS_8; //数据位
param.u8cStopBits = DRVUART_STOPBITS_1; //停止位
param.u8cParity = DRVUART_PARITY_NONE; //校验位
param.u8cRxTriggerLevel = DRVUART_FIFO_1BYTES; //FIFO存储深度 1 字节
param.u8TimeOut = 0; //FIFO超时设定
DrvUART_Open(UART_PORT0, ¶m); //串口usart0开启、结构体整体赋值
}
/*
* Function: delay_ms
* Descriptions: 1ms(晶振为12MHZ)延时子程序
* Input Parameter: count
* Output Parameter: 无
* Returns: 无
*/
void delay_ms(uint32_t count)
{
uint32_t i,j;
for(i=count;i>0;i--)
{
for(j=2395;j>0;j--);
}
}
config.h#ifndef CONFIG_H_
#define CONFIG_H_
void Set_System(void);
void RCC_Config(void);
void GPIO_Config(void);
void UART_Config(void);
void delay_ms(uint32_t count);
#endif
|