MSP430(F149)学习笔记——红外接收
时间:11-28
来源:互联网
点击:
用MSP430(F149)做红外接收比发送更为简单,我采用的红外传感器是HS0038B,这个原件在接收到38K红外时输出低电平,否则输出高电平,因此,我们就可以从这点开始编写程序了,由于HS0038B的电路图很多,我这里就不贴出来了,下面是具体的代码:
- #includemsp430x14x.h>
- #include"delay.h"
- staticunsignedshortaddr=0x00;
- staticunsignedcharir_rx_buf[256];
- staticunsignedcharir_rx_w_offset=0;
- staticunsignedcharir_rx_r_offset=0;
- voidir_tx_open(){
- P2DIR|=BIT2|BIT3;//P2.2,P2.3输出
- P2SEL|=BIT3;//P2.2:IOP2.3:TA0
- P2SEL&=~BIT2;//
- P2OUT&=~(BIT2|BIT3);
- //38K->P2.3
- CCR0=(int)(26.3*8+0.5);
- CCTL1=OUTMOD_6;
- CCR1=(int)(13.15*8+0.5);
- TACTL=TASSEL_2+MC_1;
- }
- voidir_set_addr(unsignedcharaddr){
- addr=(unsignedchar)(0xff&addr);
- }
- staticvoidir_start(){
- P2OUT|=BIT2;
- delay_us(9000);
- P2OUT&=~BIT2;
- delay_us(4500);
- }
- staticvoidir_next(){
- P2OUT|=BIT2;
- delay_us(9000);
- P2OUT&=~BIT2;
- delay_us(2250);
- }
- staticvoidir_send_byte(unsignedcharc){
- unsignedchari;
- for(i=0;i!=8;++i){
- P2OUT|=BIT2;
- delay_us(560);
- P2OUT&=~BIT2;
- if(c&0x01){
- delay_us(1685);
- }
- else{
- delay_us(565);
- }
- c>>=1;
- }
- }
- staticvoidir_end(){
- P2OUT|=BIT2;
- delay_us(300);
- P2OUT&=~BIT2;
- }
- voidir_put_char(unsignedcharc){
- ir_start();
- ir_send_byte(addr);
- ir_send_byte(~addr);
- ir_send_byte(c);
- ir_send_byte(~c);
- ir_end();
- }
- voidir_put_string(char*str){
- if(*str!=)
- ir_start();
- else
- return;
- while(1){
- ir_send_byte(addr);
- ir_send_byte(~addr);
- ir_send_byte(*str);
- ir_send_byte(~(*str));
- ++str;
- if(*str!=){
- ir_next();
- }
- else{
- ir_end();
- }
- }
- }
- voidir_put_array(unsignedchar*array,unsignedintlength){
- unsignedinti;
- ir_start();
- for(i=0;i!=length;++i){
- ir_send_byte(addr);
- ir_send_byte(~addr);
- ir_send_byte(array[i]);
- ir_send_byte(array[i]);
- if(i
- ir_next();
- }
- else{
- ir_end();
- }
- }
- }
- voidir_tx_close(){
- P2SEL&-~BIT3;
- P2DIR|=BIT3;
- P2OUT&=~BIT3;
- TACTL=TACLR;
- }
- voidir_rx_open(){
- P2SEL&=~BIT0;//I/O
- P2DIR&=~BIT0;//Input
- P2IES|=BIT0;//High->Low
- P2IE|=BIT0;//Enableinterrupt
- }
- voidir_rx_close(){
- P2IE&=~BIT0;//Disableinterrupt
- }
- unsignedcharir_get(){
- while(ir_rx_r_offset==ir_rx_w_offset);
- returnir_rx_buf[ir_rx_r_offset++];
- }
- #pragmavector=PORT2_VECTOR
- __interruptvoidport2_handler(){
- staticunsignedcharir_code[4];
- unsignedchari,j;
- unsignedshorttime;
- if(P2IFG&BIT0){
- P2IE&=~BIT0;
- P2IFG&=~BIT0;
- _EINT();
- time_start(9500);
- while(!time_out()&&!(P2IN&BIT0));
- time=time_end();
- if(!time_out()&&time>8500){
- time_start(5000);
- while(!time_out()&&(P2IN&BIT0));
- time=time_end();
- if(!time_out()&&time>4000){
- for(i=0;i!=4;++i){
- for(j=0;j!=8;++j){
- while(!(P2IN&BIT0));//等待高电平
- time_start(2000);//计时高电平
- while(!time_out()&&(P2IN&BIT0));
- time=time_end();
- ir_code[i]>>=1;
- if(!time_out()&&time>1500){
- //uart_put(0x03);
- ir_code[i]|=0x80;
- }
- else{
- //uart_put(0x04);
- ir_code[i]&=0x7f;
- }
- }
- }
- if(ir_code[0]==addr&&(0xff==ir_code[0]+ir_code[1])){
- if(0xff==ir_code[2]+ir_code[3]){
- ir_rx_buf[ir_rx_w_offset++]=ir_code[2];
- }
- }
- }
- }
- P2IE|=BIT0;;
- }
- }
MSP430学习笔记红外接 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)