⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 一个用单片机跟普通键盘的通信程序
💻 C
字号:
#include <hidef.h>      /* common defines and macros */
#include <mc9s12dg128.h>     /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12dg128b"


#define maxsize 130

unsigned char data[150];
unsigned char  data_get=0;
unsigned char count=11;
unsigned char k=0;
unsigned char data_get_b=0;


void ect_init(void){
   DDRT=0x00;       //input
   TSCR1=0x80;      //enable timer
   TIOS=0x00;       //the correspond prot as input
   TCTL4=0x08;      /*channel 1 as capture on fall edge下沿*/
   TSCR2=0x07;      //div 128 for the bus clock 
   PACTL=0x00;
}

//TIE as the contcol of the intrrupt 

void mccnt_init(void){
   MCCTL=0x04;   
   MCCNT=0;
}        

void delay_10us(unsigned char k){
    unsigned char t;
    do{
    
        for(t=80;t>0;t--); 
    
    }while(k--);
    
   


}

void sent_common(unsigned char com){
     unsigned char i;
     unsigned char j;
     TIE=0x00;
     TCTL4=0x00;
     
     DDRT=0xff;
     PTT=0x00;
     delay_10us(15);  //delay 150us
     
     DDRM=0xff;
     PTM=0x00;
    ;
     DDRT=0x00;

     TCTL4=0x08;
     //while(PTT&0x02);
     //TSCR1=0x80;
      
     // while(!TFLG1_C1F);
      //TFLG1_C1F=1;
      //PTM=0x00;
    
    
     for(i=0;i<8;i++){
      
       while(!TFLG1_C1F);
       TFLG1_C1F=1;
       if(com&0x01){
        
           PTM=0xff;
          j++;
          }
       else
          PTM=0x00;
       com>>=1;   
       
       }
     
       while(!TFLG1_C1F);
       TFLG1_C1F=1;
       if(!(j&0x01))
           PTM=0xff;
       else
          PTM=0x00;
         

       
         DDRM=0x00;
         
       for(i=0;i<2;i++){
          while(!TFLG1_C1F);
          TFLG1_C1F=1; 
       
       }


        
        TIE=0x02;
           
            


}


void main(void) {
  mccnt_init();
  ect_init();
  DDRB=0xff;
  PORTB=0x00;
  TIE=0x02;  //start the intrrupt 
  DDRM=0x00;
  EnableInterrupts;
  PTM|=0x01;
  sent_common(0xee);
  
  
  
  while(1){
  

    while(data[0]!=0xee);
    sent_common(0xff);
    while(data[1]!=0xfa);
    while(data[2]!=0xaa);
    sent_common(0xf4);
    data_get_b=0;    
    while(data[3]!=0xfa){
      if(data_get_b==0xfe) {
         sent_common(0xf4);
         data_get_b=0;
      
      }
    
    };

  
      
      while(1); 
  };
  


}

#pragma CODE_SEG NON_BANKED
void interrupt 9 data_in(void) {
    
     TFLG1_C1F=1;  //clear the flag
     TIE=0x00;
     
     if(count>1){
           
       if(count<11&&count>2){
          data_get>>=1;
          if(PTM&0x01)
          data_get|=0x80;
       }
        count--;   
          
 
      
     
     }else{
       data[k++]=data_get;
       data_get_b=data_get;
       data_get=0;
       count=11;
       if(k>maxsize)k=0;
     }
     
     

    TIE=0x02;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -