通过AD采样输出不同AD按键的电压值

#include       /* common defines and macros */
#include "derivative.h"      /* derivative-specific definitions */
#define SDA PTT_PTT1   //数据口1
#define SCL	PTT_PTT0   //时钟
#define DC  PTT_PTT3	 
#define RST	PTT_PTT2
/******************************/
void delay(uint a){
  uint b;
  for(a;a>0;a--)
    for(b=1000;b>0;b--);
}
void InitAD(void) {
  ATD0DIEN=0X00;
  ATD0CTL1=0X00;
  ATD0CTL2=0X40;
  ATD0CTL3=0X8C;
  ATD0CTL4=0X03;
  ATD0CTL5=0X20;
  
}
/***************************/
const byte tab_hanzi[][32]=             	//汉字库
{
                                   
{0x10,0x80,0x10,0x40,0x10,0x40,0x13,0xFC,0xFC,0x00,0x10,0x00,0x31,0xF0,0x39,0x10},
{0x55,0x10,0x55,0x10,0x91,0x10,0x11,0x12,0x11,0x12,0x12,0x12,0x12,0x0E,0x14,0x00},/*"杭",0*/

{0x10,0x04,0x10,0x84,0x10,0x84,0x10,0x84,0x10,0x84,0x54,0xA4,0x52,0x94,0x52,0x94},
{0x90,0x84,0x10,0x84,0x10,0x84,0x10,0x84,0x20,0x84,0x20,0x84,0x40,0x04,0x80,0x04},/*"州",1*/

{0x01,0x00,0x01,0x00,0x01,0x00,0x3F,0xF8,0x21,0x08,0x21,0x08,0x21,0x08,0x3F,0xF8},
{0x21,0x08,0x21,0x08,0x21,0x08,0x3F,0xF8,0x21,0x0A,0x01,0x02,0x01,0x02,0x00,0xFE},/*"电",2*/

{0x00,0x00,0x7F,0xF8,0x00,0x10,0x00,0x20,0x00,0x40,0x01,0x80,0x01,0x00,0xFF,0xFE},
{0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x05,0x00,0x02,0x00},/*"子",3*/

{0x08,0x10,0x1D,0x10,0xF0,0x90,0x10,0x90,0x10,0x10,0xFD,0x10,0x10,0x90,0x38,0x90},
{0x34,0x10,0x50,0x1E,0x53,0xF0,0x90,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10},/*"科",4*/

{0x10,0x20,0x10,0x20,0x10,0x20,0x13,0xFE,0xFC,0x20,0x10,0x20,0x10,0x20,0x15,0xFC},
{0x18,0x84,0x30,0x88,0xD0,0x48,0x10,0x50,0x10,0x20,0x10,0x50,0x51,0x88,0x26,0x06},/*"技",5*/

{0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0xFF,0xFE,0x01,0x00,0x01,0x00},
{0x02,0x80,0x02,0x80,0x04,0x40,0x04,0x40,0x08,0x20,0x10,0x10,0x20,0x08,0xC0,0x06},/*"大",6*/

{0x22,0x08,0x11,0x08,0x11,0x10,0x00,0x20,0x7F,0xFE,0x40,0x02,0x80,0x04,0x1F,0xE0},
{0x00,0x40,0x01,0x80,0xFF,0xFE,0x01,0x00,0x01,0x00,0x01,0x00,0x05,0x00,0x02,0x00},/*"学",7*/

};
/*********************************/
const byte tab_num[][16]=                //数字字模
{ 

{0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00},/*"0",0*/

{0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00},/*"1",1*/

{0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00},/*"2",2*/

{0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00},/*"3",3*/

{0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00},/*"4",4*/

{0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00},/*"5",5*/

{0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00},/*"6",6*/

{0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00},/*"7",7*/

{0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00},/*"8",8*/

{0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00},/*"9",9*/

};
/************************************/
const byte other_char[][16]=    	
{

{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*" ",0*/

{0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x00},/*"=",1*/

{0xC0,0x30,0xC8,0x28,0xE8,0x10,0xE0,0x00,0x07,0x18,0x27,0x24,0x23,0x14,0x0B,0x00},/*"@",2*/

{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01},/*"-",3*/

{0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x00,0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x00},/*"<",4*/

{0x00,0x08,0x10,0x20,0x40,0x80,0x00,0x00,0x00,0x20,0x10,0x08,0x04,0x02,0x01,0x00},/*">",5*/

{0x00,0x70,0x48,0x08,0x08,0x08,0xF0,0x00,0x00,0x00,0x00,0x30,0x36,0x01,0x00,0x00},/*"?",6*/

{0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x33,0x30,0x00,0x00,0x00},/*"!",7*/

{0x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00,0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00},/*"*",8*/

{0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x1F,0x01,0x01,0x01,0x00},/*"+",9*/

{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80},/*"_",10*/

{0x00,0x00,0x00,0x00,0x80,0x60,0x18,0x04,0x00,0x60,0x18,0x06,0x01,0x00,0x00,0x00},/*"/",11*/

{0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00},/*":",12*/

{0xF0,0x08,0xF0,0x00,0xE0,0x18,0x00,0x00,0x00,0x21,0x1C,0x03,0x1E,0x21,0x1E,0x00},/*"%",13*/

{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x06,0x00,0x00},/*".",14*/

};   
void wrdata(byte data){
  byte i=8;
  DC=1;
  SCL=0;
  while(i--){
    if(data&0x80)
      SDA=1;
    else
      SDA=0;
    SCL=1;
    _asm(nop);
    SCL=0;
    data<<=1;
  }
  
}
void wrcmd(byte cmd){
  byte i=8;
  DC=0;
  SCL=0;
  while(i--){
    if(cmd&0x80)
      SDA=1;
    else
      SDA=0;
    SCL=1;
    _asm(nop);
    SCL=0;
    cmd<<=1;
  }
}
/*void display_on(void)
{
    wrcmd(0X8D);  //SET DCDC命令
    wrcmd(0X14);  //DCDC ON
    wrcmd(0XAF);  //DISPLAY ON
}

void display_off(void)
{
    wrcmd(0X8D);  //SET DCDC命令
    wrcmd(0X10);  //DCDC OFF
    wrcmd(0XAE);  //DISPLAY OFF
} 
  */
void set_xy(byte x,byte y){
  wrcmd((y|0xb0));
  wrcmd((x&0x0f));
  wrcmd(((x&0xf0)>>4)|0x10);
}
void clear_oled(void)  
{  
   byte page,x;
   set_xy(0,0);
    for(page=0;page<9;page++)
    {
        for(x=0;x<128;x++){wrdata(0x00);}
        set_xy(0,page);
    } 
}

void init_oled(void){
  DDRT=0XFF;
  SCL=1;
  RST=0;
  _asm(nop);
  _asm(nop);
  RST=1;
  wrcmd(0xae);
  wrcmd(0xD5);
  wrcmd(0x80);
  wrcmd(0xA8);
  wrcmd(0x3f);
  wrcmd(0xd3);
  wrcmd(0x00);
  wrcmd(0x40);
  wrcmd(0x8d);
  wrcmd(0x14);
  wrcmd(0x20);
  wrcmd(0x02);
  wrcmd(0xa1);
  wrcmd(0xc8);
  wrcmd(0xea);
  wrcmd(0x12);
  wrcmd(0x81);
  wrcmd(0xef);
  wrcmd(0xd9);
  wrcmd(0xf1);
  wrcmd(0xdb);
  wrcmd(0x30);
  wrcmd(0xa4);
  wrcmd(0xa6);
  wrcmd(0xaf);
  clear_oled();
} 


void display_hanzi(byte x,byte y,byte w) {
    byte c;
    set_xy(x,y);
    for(c=0;c<16;c++)   {  wrdata(tab_hanzi[w][c]);  }
    set_xy(x,y+1);
    for(c=16;c<32;c++)  {  wrdata(tab_hanzi[w][c]);  }
}
void display_num(byte x,byte y,byte w) {
    byte c;
    set_xy(x,y);
    for(c=0;c<8;c++)   {  wrdata(tab_num[w][c]);  }
    set_xy(x,y+1);
    for(c=8;c<16;c++)  {  wrdata(tab_num[w][c]);  }
}
void display_other(byte x,byte y,byte w) {
    byte c;
    set_xy(x,y);
    for(c=0;c<8;c++)   {  wrdata(other_char[w][c]);  }
    set_xy(x,y+1);
    for(c=8;c<16;c++)  {  wrdata(other_char[w][c]);  }
}  



void main(void) {
  unsigned char v;
  uint t1,t2,t3,t;
  InitAD();
  init_oled();
	//EnableInterrupts;
  for(;;) {
     while(!ATD0STAT2L_CCF0){
    ;}
    v=ATD0DR0L;
    t=v*100/255*5;
    t1=t/100;
    t2=(t-t1*100)/10;
    t3=t-t1*100-t2*10;
      display_num(16,0,t1);
      display_other(24,0,14);
      display_num(32,0,t2);
      display_num(40,0,t3);
     
    _FEED_COP(); /* feeds the dog */
  } 
}

你可能感兴趣的:(MC9S128)