这一节实现利用中断实现串口的中断功能,关于串口的原理我就不再讲述了,如果不明白,就请查看我的另一篇博客 http://blog.csdn.net/mybelief321/article/details/8931064 下面我还是贴出自己的实验代码,我上传到了网站,可以自行下载,下载后,打开工程文档,直接编译后,利用Flash/Dowmload功能下载到nor flash,关于下载到nor flash如果有不会的请看:http://blog.csdn.net/mybelief321/article/details/8954788 本实验实现的功能是:利用串口中断功能,通过串口调试工具接收到字符后再显示。 下图为我的工程文档 main.c文件 #include"isrservice.h" #include"uart.h" #include"led.h" #include"interrupt.h" int main() { Uart0_Init(115200); //初始化并设置波特率为115 200 Uart0_Interrupt_Init(); //Uart0中断初始化 Led_Init(); //Led初始化 while(1) //循环,等到中断发生 { ; } } led.h文件 #ifndef __LED_H__ #define __LED_H__ #include<s3c2440.h> #define Led1_On() {GPBDAT&=(~(1<<5));} #define Led1_Off() {GPBDAT|=(1<<5);} #define Led2_On() {GPBDAT&=(~(1<<6));} #define Led2_Off() {GPBDAT|=(1<<6);} #define Led3_On() {GPBDAT&=(~(1<<7));} #define Led3_Off() {GPBDAT|=(1<<7);} #define Led4_On() {GPBDAT&=(~(1<<8));} #define Led4_Off() {GPBDAT|=(1<<8);} /**************************************************** * 函数名称:void Led_Init(void) * 全局变量:无 * 参数说明:无 * 返 回 值;无 * 功 能:设置GPN5-8为输出功能,初始化4个LED灯灭 *****************************************************/ void Led_Init(void); #endif led.c文件 /**************************************************** * 我的mini2440开发板上4个LED灯对应的GPIO口 * LED1---GPB5 LED2---GPB6 * LED3---GPB7 LED4---GPB8 *****************************************************/ #include<s3c2440.h> /**************************************************** * 函数名称:void Led_Init(void) * 全局变量:无 * 参数说明:无 * 返 回 值;无 * 功 能:设置GPB5-8为输出功能,初始化4个LED灯灭 *****************************************************/ void Led_Init(void) { GPBCON&=~((3<<10)|(3<<12)|(3<<14)|(3<<16)); GPBCON|=((1<<10)|(1<<12)|(1<<14)|(1<<16)); //设置GPB5-8口为输出功能 GPBUP&=~((1<<5)|(1<<6)|(1<<7)|(1<<8)); //上拉电阻使能 GPBDAT|=(1<<5)|(1<<6)|(1<<7)|(1<<8); //令GPBDAT5-8均为高电平,即令4个led灯全灭 } uart.h文件 #ifndef __UART_H__ #define __UART_H__ /*********************************************** *函数名称:void Uart0_Init(unsigned int baudrate) *参数说明:baudrate:波特率 *返 回 值:无 *全局变量: 无 *功 能:对UART0进行初始化 ************************************************/ void Uart0_Init(unsigned int baudrate); #endif uart.c文件 #include<s3c2440.h> #include<stdarg.h> #include"uart.h" #define PCLK 50000000 #define UART_BRD (int)((PCLK/(baudrate*16))-1) /*********************************************** *函数名称:void Uart0_Init(unsigned int baudrate) *参数说明:baudrate:波特率 *返 回 值:无 *全局变量: 无 *功 能:对UART0进行初始化 ************************************************/ void Uart0_Init(unsigned int baudrate) { GPHCON&=~((3<<4)|(3<<6)); //GPH2--TXD0;GPH3--RXD0 GPHCON|=((2<<4)|(2<<6)); //设置GPH2、GPH3为TXD0、RXD0功能 GPHUP=0x00; //上拉电阻使能 ULCON0|=0x03; //设置数据发送格式:8个数据位,1个停止位,无校验位 UCON0=0x05; //发送模式和接收模式都使用查询模式 UBRDIV0=UART_BRD; //设置波特率,其中波特率作为一个参数传递到该初始化函数 URXH0=0; //将URXH0清零 } interrupt.h文件 #ifndef __INTERRUPT_H__ #define __INTERRUPT_H__ /**************************************************** * 函数名称:void Uart0_Interrupt_Init(void) * 全局变量:无 * 参数说明:无 * 返 回 值;无 * 功 能:将UART0中断屏蔽位设为无效 *****************************************************/ void Uart0_Interrupt_Init(void); #endif interrupt.c文件 #include<s3c2440.h> #include"interrupt.h" /**************************************************** * 函数名称:void Uart0_Interrupt_Init(void) * 全局变量:无 * 参数说明:无 * 返 回 值;无 * 功 能:将UART0中断屏蔽位设为无效 *****************************************************/ void Uart0_Interrupt_Init(void) { INTMSK&=~(1<<28); //Uart0中断带有子中断,所以需要将 //Uart0总中断屏蔽位置为无效,然后将发送 //中断和接收中断屏蔽位置设为无效, INTSUBMSK&=~((1<<0)|(1<<1)); //这样程序才能顺利地响应发送中断和接收 } isrservice.h文件 #ifndef __ISRSERVICE_H__ #define __ISRSERVICE_H__ /**************************************************** * 函数名称:void __irq IRQ_Handler(void) * 全局变量:无 * 参数说明:无 * 返 回 值;无 * 功 能:Uart0中断服务函数,必须加__irq *****************************************************/ void __irq IRQ_Handler(void); #endif isrservice.c文件 #include<s3c2440.h> #include"isrservice.h" #include"led.h" extern unsigned int flag; //声明外部变量flag,该变量是在main.c文件中定义的 //当1s到来时,中断响应函数将该变量值取反,在主 //程序中通过检测该变量的值来实现不同的操作 /**************************************************** * 函数名称:void __irq IRQ_Handler(void) * 全局变量:无 * 参数说明:无 * 返 回 值;无 * 功 能:Uart0中断服务函数,必须加__irq *****************************************************/ void __irq IRQ_Handler(void) //注意这个函数名字要和S3C2440.s处的跳转标号相同 { unsigned char buf; if(SUBSRCPND&(1<<0)) //接收中断 { buf=URXH0; //将接收到的字符存放在buf中 Led1_On(); SUBSRCPND|=1<<0; //清除接收中断 SRCPND|=1<<28; INTPND|=1<<28; UTXH0=buf; //PC机将接收的字符通过串口调试工具显示在屏幕上 } if(SUBSRCPND&(1<<1)) //发送中断 { Led2_On(); SUBSRCPND|=1<<1; //清除发送中断 SRCPND|=1<<28; INTPND|=1<<28; } }

浙公网安备 33010602011771号