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

📄 com_not_ok.c

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




unsigned char data[150];
unsigned char  data_get=0;
unsigned char count=11;
unsigned char k=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;
     unsigned char g=30;
     TIE=0x00;
     TCTL4=0x00;
     
     DDRT|=0x02;
     PTT&=0xfd;
     delay_10us(15);  //delay 150us
     
     DDRM|=0x01;
     PTM&=0xfe;
     
     DDRT=0x00;
     
     TCTL4=0x08;
     //while(PTT&0x02);
     //TSCR1=0x80; 
     while(!TFLG1_C1F);
     TFLG1_C1F=1;
     PTM&=0xfe; //set 0
     //TSCR1=0x80; 
    
     for(i=0;i<8;i++){
      
       while(!TFLG1_C1F);
       TFLG1_C1F=1;
       if(com&0x01){
        
          PTM&=0xff; //set 1 
          j++;
          }
       else
          PTM&=0xfe; //set 0
       com>>=1;   
       
       }
     
       while(!TFLG1_C1F);
       TFLG1_C1F=1;
       if(!(j&0x01))
           PTM&=0xff; //set 1 
       else
           PTM&=0xfe; //set 0
         
       while(!TFLG1_C1F);
       TFLG1_C1F=1;
            PTM&=0xff; //set 1 
            
            
        DDRM&=0xfe;
    //   while(!TFLG1_C1F);
        
        while(k<100){
           while(!TFLG1_C1F);
           TFLG1_C1F=1;
           if(PTM&0x01)
           data[k++]=1;
           else
           data[k++]=0;
           
           
         
        
        }
        
        
        
        TIE=0x02;
           
            


}


void main(void) {
  mccnt_init();
  ect_init();
  TIE=0x02;  //start the intrrupt 
  DDRM=0x00;
  EnableInterrupts;
  PTM|=0x01;
  
  while(1){
    if(k==3) 
    sent_common(0xee);
  
  };
  


}

#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=0;
       count=11;
     }
     
     

    TIE=0x02;
}

⌨️ 快捷键说明

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