找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3313|回复: 0
打印 上一主题 下一主题
收起左侧

定位显示系统msp430程序

[复制链接]
跳转到指定楼层
楼主
定位显示系统msp430程序

  1. #include "msp430g2553.h"
  2. #include "math.h"
  3. #define SENDCOMM 0XF8     //0XF1000
  4. #define READCOMM 0XFC     //0XF1100
  5. #define SENDDATA 0XFA     //0XF1010
  6. #define READDATA 0XFE     //0XF1110
  7. /*-----12864串行定义--------*/
  8. #define E_CLK_0  P1OUT &= ~BIT7         //时钟信号为0
  9. #define E_CLK_1  P1OUT |= BIT7                //时钟信号为1
  10. #define RW_SID_0  P1OUT &= ~BIT6        //数据信号为0
  11. #define RW_SID_1  P1OUT |= BIT6                //数据信号为1
  12. /*-----12864工作方式定义-------*/
  13. #define EXTEND_SET 0x34//扩展指令集,绘图显示关
  14. #define DRAW_ON 0x36 //绘图显示开
  15. #define DRAW_OFF 0x34 //绘图显示关
  16. #define BASIC_SET 0x30//基本指令集

  17. #define FOSC 16000000
  18. #define Machine_cycle 1.0/FOSC
  19. #define Sound 340
  20. /*------LCD12864基本函数-------*/
  21. void LCD_send_byte(unsigned char a);
  22. unsigned char LCD_read_byte();
  23. void LCD_write_com(unsigned char comm);
  24. void LCD_write_data(unsigned char lcd_data);
  25. void LCD_set_addr(unsigned char x, unsigned char y);
  26. void LCD_write_string(unsigned char addr_x, unsigned char addr_y, char* str);
  27. void LCD_list_num(unsigned char x,unsigned char y,long number);
  28. void LCD_list_Decimal(unsigned char x,unsigned char y,double Decimal);
  29. void LCD_init(void);
  30. void LCD_write_char(unsigned char x, unsigned char y,unsigned char flag, char data);
  31. void LCD_clear(void);
  32. unsigned char LCD_read_data();
  33. void check_busy();
  34. /*-----LCD12864绘图函数-------*/
  35. void GUI_clear();  //绘图清屏函数 (清屏指令在画图时不能使用)
  36. void GUI_draw_full_picture(const unsigned char *dat);//画满屏图片,横向取模
  37. //void LCD_set_dot(unsigned char x,unsigned char y);//打点程序
  38. //void GUI_hline(unsigned char x0,unsigned char x1,unsigned char y);//画水平线函数,x0,x1为起始点和终点的水平坐标,y为垂直坐标
  39. /*---------键盘函数---------*/
  40. unsigned char getkey(void);
  41. void GPIO_init();
  42. void TIMER0_init();
  43. void TIMER1_init();
  44. void Calculation();

  45. unsigned char num[] = {"0123456789ABCDEF"};
  46. unsigned char key_t,key,t;
  47. unsigned char odd;
  48. unsigned long cap1_2=0;
  49. unsigned long cap2_0=0;
  50. unsigned long cap2_1=0;
  51. unsigned long cap2_4=0;
  52. unsigned int flag=0;
  53. double x=0,y=0;
  54. double X[20];
  55. double Y[20];

  56. void main(void) {
  57.         WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
  58.         DCOCTL=CALDCO_16MHZ;
  59.         BCSCTL1=CALBC1_16MHZ;
  60.         IFG1 &= ~OFIFG; // Clear OSCFault flag
  61.         LCD_init();
  62.         LCD_clear();
  63.         GPIO_init();
  64.         TIMER0_init();
  65.         TIMER1_init();
  66.         LCD_write_string(1,8,"坐标");
  67.         _EINT();//开全局中断
  68.         while(1)
  69.         {
  70.                 if(cap1_2!=0)
  71.                         if(cap2_0!=0)
  72.                                 if(cap2_1!=0)
  73.                                         if(cap2_4!=0)
  74.                                         {
  75.                                                 _DINT();
  76.                                                 TA0CTL&=~MC_2;
  77.                                                 TA1CTL&=~MC_2;
  78.                                                 Calculation();
  79.                                                 cap1_2=0;
  80.                                                 cap2_0=0;
  81.                                                 cap2_1=0;
  82.                                                 cap2_4=0;
  83.                                                 TA0CCR1=0x0000;
  84.                                                 TA1CCR0=0x0000;
  85.                                                 TA1CCR1=0x0000;
  86.                                                 TA1CCR2=0x0000;
  87.                                                 P1SEL&=~BIT2;
  88.                                                 P1OUT&=~BIT2;
  89.                                                 _delay_cycles(10);
  90.                                                 P1IE|=BIT2;
  91.                                                 _EINT();
  92.                                         }
  93.         }
  94. }
  95. //P1口外部中断
  96. #pragma vector=PORT1_VECTOR
  97. __interrupt void Port_1(void)
  98. {
  99.         if(P1IFG&BIT2)
  100.         {
  101.                 P1OUT^=BIT0;
  102.                 P1IE&=~BIT2;  //1.2禁止中断
  103.                 P1OUT|=BIT2; //1.2输出
  104.                 P1SEL|=BIT2; //p1.2外围模块功能
  105.                 _delay_cycles(10);
  106.                 TA0CTL|=MC_2+TACLR; //MC2连续计数   TACLR清除
  107.                 TA1CTL|=MC_2+TACLR;
  108.                 TA0CCTL1|=CM_2+CCIE;//CM选择捕获  CCIE中断允许
  109.                 TA1CCTL0|=CM_2+CCIE;
  110.                 TA1CCTL1|=CM_2+CCIE;
  111.                 TA1CCTL2|=CM_2+CCIE;
  112.                 P1IFG&=~BIT2;        //P1.2无中断请求
  113.         }
  114. }

  115. // Timer A0 interrupt service routine
  116. #pragma vector=TIMER0_A1_VECTOR                           //A1定时  R0计数
  117. __interrupt void Timer0_A1(void)
  118. {
  119.         TA0CCTL1&=~CM_2;
  120.         TA0CCTL1&=~CCIE;
  121.         P1OUT^=BIT0;
  122.         cap1_2=TA0CCR1;
  123.         TA0CCTL1&=~CCIFG;
  124. }

  125. // Timer A1 interrupt service routine
  126. #pragma vector=TIMER1_A0_VECTOR       
  127. __interrupt void Timer1_A0(void)                                //A0定时  R1计数
  128. {
  129.         TA1CCTL0&=~CM_2;
  130.         TA1CCTL0&=~CCIE;
  131.         P1OUT^=BIT0;
  132.         cap2_0=TA1CCR0;
  133.         TA1CCTL0&=~CCIFG;
  134. }

  135. #pragma vector=TIMER1_A1_VECTOR                                //A1定时  R1计数
  136. __interrupt void Timer1_A1(void)
  137. {
  138.         switch(TA1IV)
  139.         {
  140.         case TA1IV_TACCR1:
  141.                 {
  142.                         TA1CCTL1&=~CM_2;
  143.                         TA1CCTL1&=~CCIE;
  144.                         P1OUT^=BIT0;
  145.                         cap2_1=TA1CCR1;
  146.                         TA1CCTL1&=~CCIFG;
  147.                         break;
  148.                 }
  149.         case TA1IV_TACCR2:
  150.                 {
  151.                         TA1CCTL2&=~CM_2;
  152.                         TA1CCTL2&=~CCIE;
  153.                         P1OUT^=BIT0;
  154.                         cap2_4=TA1CCR2;
  155.                         TA1CCTL2&=~CCIFG;
  156.                         break;
  157.                 }
  158.         default:break;
  159.         }
  160. }

  161. void GPIO_init()
  162. {
  163.         //标志口
  164.         P1DIR|=BIT0;
  165.         P1OUT&=~BIT0;
  166.         _delay_cycles(20);
  167.         //p1.2外部中断设置
  168.         P1DIR&=~BIT2;//输入,上升沿触发,开中断
  169.         P1REN|=BIT2;
  170.         P1OUT&=~BIT2;
  171.         P1IES&=~BIT2;
  172.         P1IE|=BIT2;
  173.         P1IFG&=~BIT2;
  174.         _delay_cycles(20);
  175.         //p2.0 p2.1 p2.4捕获设置
  176.         P2DIR&=~BIT0+BIT1+BIT4;//设置P2.0,2.1,2.4为输入
  177.         P2REN|=BIT0+BIT1+BIT4;
  178.         P2OUT|=BIT0+BIT1+BIT4;
  179.         P2SEL|=BIT0+BIT1+BIT4;
  180.         _delay_cycles(20);
  181. }

  182. void TIMER0_init()
  183. {
  184.         TA0CTL |= TASSEL_2+MC_0+TACLR;
  185.         TA0CCTL1|=CCIS_0+SCS+CAP+CM_0;
  186. }

  187. void TIMER1_init()
  188. {
  189.         TA1CTL |= TASSEL_2+MC_0+TACLR;
  190.         TA1CCTL0|=CCIS_0+SCS+CAP+CM_0;
  191.         TA1CCTL1|=CCIS_0+SCS+CAP+CM_0;
  192.         TA1CCTL2|=CCIS_0+SCS+CAP+CM_0;
  193. }

  194. void Calculation()
  195. {
  196.         long a,b,c,d;
  197.         double DA,DB,DC,DD;
  198.         double x1,x2,y1,y2;
  199.         DA=Sound*cap1_2*Machine_cycle*100.0;
  200.         DB=Sound*cap2_0*Machine_cycle*100.0;
  201.         DC=Sound*cap2_1*Machine_cycle*100.0;
  202.         DD=Sound*cap2_4*Machine_cycle*100.0;
  203.         x1=(DD*DD-DC*DC+250000*1.0)/(1000*1.0);
  204.         x2=(DA*DA-DB*DB+250000*1.0)/(1000*1.0);
  205.         y1=(DB*DB-DC*DC+122500*1.0)/(700*1.0);
  206.         y2=(DA*DA-DD*DD+122500*1.0)/(700*1.0);
  207.         if(flag<20)
  208.         {
  209.                 X[flag]=(x1+x2)*1.0/2.0;
  210.                 Y[flag]=(y1+y2)*1.0/2.0;
  211.                 flag++;
  212.         }
  213.         else
  214.         {
  215.                 for(flag=0;flag<20;flag++)
  216.                 {
  217.                         x=x+X[flag];
  218.                         y=y+Y[flag];
  219.                 }
  220.                 x=x*1.0/20.0;
  221.                 y=y*1.0/20.0;
  222.                 LCD_list_Decimal(2,4,x);
  223.                 LCD_list_Decimal(3,4,y);
  224.                 flag=0;
  225.         }
  226. //        a=cap1_2-cap1_2;
  227. //        b=cap2_0-cap1_2;
  228. //        c=cap2_1-cap1_2;
  229. //        d=cap2_4-cap1_2;
  230. //        LCD_list_num(1,1,a);
  231. //        LCD_list_num(2,1,b);
  232. //        LCD_list_num(3,1,c);
  233. //        LCD_list_num(4,1,d);
  234.         LCD_list_num(1,1,cap1_2);
  235.         LCD_list_num(2,1,cap2_0);
  236.         LCD_list_num(3,1,cap2_1);
  237.         LCD_list_num(4,1,cap2_4);
  238. }
  239. /*---------键盘函数---------*/
  240. unsigned char getkey(void) //获得键盘的值
  241. {
  242.         P1REN |= BIT3 + BIT4 + BIT5;
  243.         P1DIR &= ~(BIT3 + BIT4 + BIT5);
  244.         P1OUT |= BIT3 + BIT4 + BIT5; //00111000
  245.         t = (P1IN & 0x38)/8;
  246.         if (t == 0x07) {
  247.                 P1REN |= BIT4 + BIT5;
  248.                 P1REN &= ~BIT3;
  249.                 P1DIR |= BIT3 + BIT4 + BIT5;
  250.                 P1OUT |= BIT4 + BIT5; //00110000
  251.                 P1OUT &= ~BIT3;
  252.                 P1DIR &= ~(BIT4 + BIT5);
  253.                 t = P1IN & 0x038;
  254.                 t = (t/8) / 2 + 7;
  255.                 if (t == 0x0a) {
  256.                         P1REN |= BIT5 + BIT3;
  257.                         P1REN &= ~BIT4;
  258.                         P1DIR |= BIT3 + BIT4 + BIT5;
  259.                         P1OUT |= BIT3+BIT5; //00101000
  260.                         P1OUT &= ~BIT4;
  261.                         P1DIR &= ~(BIT3 + BIT5);
  262.                         t = P1IN & 0x38;
  263.                         t = ((t/8) + 1) / 2 + 10;
  264.                         if (t == 0x0d) {
  265.                                 P1REN |= BIT3 + BIT4;
  266.                                 P1REN &= ~BIT5;
  267.                                 P1DIR |= BIT3 + BIT4 + BIT5;
  268.                                 P1OUT |= BIT3+BIT4; //00011000
  269.                                 P1OUT &= ~BIT5;
  270.                                 P1DIR &= ~(BIT3 + BIT4);
  271.                                 t = P1IN & 0x38;
  272.                                 t =(t/8)+13;
  273.                         }
  274.                 }
  275.         }
  276.         return (t);
  277. }

  278. void show_key()//显示键盘的值
  279. {
  280.         key_t=getkey();
  281.         if(key_t!=16)
  282.         {
  283.                 _delay_cycles(20000);
  284.                 key=getkey();
  285.                 if(key==key_t)
  286.                 {
  287.                         while(1)
  288.                         {
  289.                                 key_t=getkey();
  290.                                 if(key_t==16)
  291.                                         break;
  292.                         }
  293.                 }
  294.                 else
  295.                 {
  296.                         key=16;
  297.                 }
  298.                 if(key==15)
  299.                         LCD_clear();
  300.                 else
  301.                         LCD_write_char(1, 2,0,num[key]);
  302.                 _delay_cycles(200000);
  303.         }
  304. }

  305. /*------LCD12864基本函数-------*/
  306. void check_busy()
  307. {
  308.         do
  309.         {
  310.                 LCD_send_byte(READCOMM);
  311.         }
  312.         while(LCD_read_byte()&0x80);
  313. }

  314. void LCD_send_byte(unsigned char a) {
  315.         unsigned char i=0;
  316.         for (i = 8; i > 0; i--) {
  317.                 if (a & (0x01 << (i - 1)))
  318.                         RW_SID_1;
  319.                 else
  320.                         RW_SID_0;
  321.                 _delay_cycles(100);
  322.                 E_CLK_1;
  323.                 _delay_cycles(100);
  324.                 E_CLK_0;
  325.                 _delay_cycles(100);
  326.         }
  327. }

  328. unsigned char LCD_read_byte()
  329. {
  330.         unsigned char i,j,temp1=0;
  331.         _delay_cycles(100);
  332.         P1DIR&=~BIT6;
  333.         RW_SID_1;
  334.         for(j=0;j<2;j++)
  335.         {
  336.                 for(i=0;i<4;i++)
  337.                 {
  338.                         if(P1IN&BIT6) temp1|=0x01;
  339.                         else temp1&=~0x01;
  340.                         temp1<<=1;
  341.                         E_CLK_0;
  342.                         _delay_cycles(100);
  343.                         E_CLK_1;
  344.                         _delay_cycles(100);
  345.                         E_CLK_0;
  346.                         _delay_cycles(100);
  347.                 }
  348.                 for(i=0;i<4;i++)
  349.                 {
  350.                         E_CLK_0;
  351.                         _delay_cycles(100);
  352.                         E_CLK_1;
  353.                         _delay_cycles(100);
  354.                         E_CLK_0;
  355.                         _delay_cycles(100);
  356.                 }
  357.         }
  358.         P1DIR|=BIT6;
  359.         return temp1;
  360. }

  361. unsigned char LCD_read_data()
  362. {
  363.         unsigned char temp;
  364.         LCD_send_byte(READDATA);
  365.         _delay_cycles(1000);
  366.         temp=LCD_read_byte();
  367.         return temp;
  368. }

  369. void LCD_write_com(unsigned char comm) {
  370.         unsigned char temp;
  371.         check_busy();
  372.         _delay_cycles(100);

  373.         LCD_send_byte(SENDCOMM); //MCU to LCD, command

  374.         temp = comm & 0XF0;
  375.         LCD_send_byte(temp); //send high 4 bits

  376.         temp = ((comm & 0X0F) << 4) & 0XF0;
  377.         LCD_send_byte(temp); //send low 4 bits

  378.         _delay_cycles(100);

  379. }

  380. void LCD_write_data(unsigned char lcd_data) //写数据
  381. {
  382.         unsigned char temp;
  383.         check_busy();
  384.         _delay_cycles(100);
  385.         LCD_send_byte(SENDDATA);

  386.         temp = lcd_data & 0xf0;
  387.         LCD_send_byte(temp);

  388.         temp = (lcd_data & 0X0F) << 4;
  389.         LCD_send_byte(temp);

  390.         _delay_cycles(100);

  391. }

  392. void LCD_set_addr(unsigned char x, unsigned char y) //设置显示位置
  393. {
  394.         if(y%2==0) odd=0;
  395.         else odd=1;
  396.         y=(y+1)/2;
  397.         switch (x) {
  398.         case 1:
  399.                 LCD_write_com(0X7F + y);
  400.                 break;
  401.         case 2:
  402.                 LCD_write_com(0X8F + y);
  403.                 break;
  404.         case 3:
  405.                 LCD_write_com(0X87 + y);
  406.                 break;
  407.         case 4:
  408.                 LCD_write_com(0X97 + y);
  409.                 break;
  410.         default:
  411.                 break;
  412.         }
  413. }

  414. void LCD_write_char(unsigned char x, unsigned char y,unsigned char flag, char data)
  415. {
  416.         LCD_set_addr(x, y);
  417.         if(odd==0)
  418.         {
  419.                 if(flag==1)
  420.                 {
  421.                         LCD_read_data();
  422.                         LCD_set_addr(x,y);
  423.                         LCD_write_data(data);
  424.                 }
  425.         }
  426.         LCD_write_data(data);
  427. }

  428. void LCD_write_string(unsigned char addr_x, unsigned char addr_y, char* str) {
  429.         unsigned char LCD_temp;
  430.         LCD_set_addr(addr_x, addr_y);
  431.         if(odd==0)
  432.         {
  433.                 LCD_read_data();
  434.                 LCD_set_addr(addr_x,addr_y);
  435.                 LCD_write_data(*str++);
  436.         }
  437.         LCD_temp = *str;
  438.         while (LCD_temp != 0x00) {
  439.                 LCD_write_data(LCD_temp);
  440.                 LCD_temp = *(++str);
  441.         }
  442. }

  443. void LCD_list_num(unsigned char x,unsigned char y,long number)
  444. {
  445.         unsigned char pos;
  446.         switch (x)
  447.         {
  448.                 case 1:
  449.                         x=0x7F;
  450.                         break;

  451.                 case 2:
  452.                         x=0x8F;
  453.                         break;

  454.                 case 3:
  455.                         x=0x87;
  456.                         break;

  457.                 case 4:
  458.                         x=0x97;
  459.                         break;
  460.         }
  461.         pos=x+y;
  462.         LCD_write_com(pos);
  463.         if(number<=-100000&&number>-1000000)
  464.         {
  465.                 LCD_write_data(0x30+(-number)/100000);
  466.                 LCD_write_data(0x30+((-number)/10000)%10);
  467.                 LCD_write_data(0x30+((-number)/1000)%10);
  468.                 LCD_write_data(0x30+((-number)/100)%10);
  469.                 LCD_write_data(0x30+((-number)/10)%10);
  470.                 LCD_write_data(0x30+(-number)%10);
  471.         }
  472.         if(number<=-10000&&number>-100000)
  473.         {
  474.                 LCD_write_data(0x30+(-number)/10000);
  475.                 LCD_write_data(0x30+((-number)/1000)%10);
  476.                 LCD_write_data(0x30+((-number)%1000)/100);
  477.                 LCD_write_data(0x30+((-number)%100)/10);
  478.                 LCD_write_data(0x30+(-number)%10);
  479.         }
  480.         if(number<=-1000&&number>-10000)
  481.         {
  482.                 LCD_write_data('-');
  483.                 LCD_write_data(0x30+(-number)/1000);
  484.                 LCD_write_data(0x30+((-number)/100)%10);
  485.                 LCD_write_data(0x30+((-number)%100)/10);
  486.                 LCD_write_data(0x30+(-number)%10);
  487.         }
  488.         if(number<=-100&&number>-1000)
  489.         {
  490.                 LCD_write_data('-');
  491.                 LCD_write_data(0x30+(-number)/100);
  492.                 LCD_write_data(0x30+((-number)/10)%10);
  493.                 LCD_write_data(0x30+(-number)%10);
  494.         }
  495.         if(number<=-10&&number>-100)
  496.         {
  497.                 LCD_write_data('-');
  498.                 LCD_write_data(0x30+(-number)/10);
  499.                 LCD_write_data(0x30+(-number)%10);
  500.         }
  501.         if(number<0&&number>-10)
  502.         {
  503.                 LCD_write_data('-');
  504.                 LCD_write_data(0x30+(-number)%10);
  505.         }
  506.         if(number>=0&&number<10)
  507.         {
  508.                 LCD_write_data(0x30+number%10);
  509.         }
  510.         if(number>=10&&number<100)
  511.         {
  512.                 LCD_write_data(0x30+number/10);
  513.                 LCD_write_data(0x30+number%10);
  514.         }
  515.         if(number>=100&&number<1000)
  516.         {
  517.                 LCD_write_data(0x30+number/100);
  518.                 LCD_write_data(0x30+(number/10)%10);
  519.                 LCD_write_data(0x30+number%10);
  520.         }
  521.         if(number>=1000&&number<10000)
  522.         {
  523.                 LCD_write_data(0x30+number/1000);
  524.                 LCD_write_data(0x30+(number/100)%10);
  525.                 LCD_write_data(0x30+(number%100)/10);
  526.                 LCD_write_data(0x30+number%10);
  527.         }
  528.         if(number>=10000&&number<100000)
  529.         {
  530.                 LCD_write_data(0x30+number/10000);
  531.                 LCD_write_data(0x30+(number/1000)%10);
  532.                 LCD_write_data(0x30+(number%1000)/100);
  533.                 LCD_write_data(0x30+(number%100)/10);
  534.                 LCD_write_data(0x30+number%10);
  535.         }
  536.         if(number>=100000&&number<1000000)
  537.         {
  538.                 LCD_write_data(0x30+number/100000);
  539.                 LCD_write_data(0x30+(number/10000)%10);
  540.                 LCD_write_data(0x30+(number/1000)%10);
  541.                 LCD_write_data(0x30+(number/100)%10);
  542.                 LCD_write_data(0x30+(number/10)%10);
  543.                 LCD_write_data(0x30+number%10);
  544.         }
  545.         if(number>=1000000&&number<10000000)
  546.         {
  547.                 LCD_write_data(0x30+number/1000000);
  548.                 LCD_write_data(0x30+(number/100000)%10);
  549.                 LCD_write_data(0x30+(number/10000)%10);
  550.                 LCD_write_data(0x30+(number/1000)%10);
  551.                 LCD_write_data(0x30+(number/100)%10);
  552.                 LCD_write_data(0x30+(number/10)%10);
  553.                 LCD_write_data(0x30+number%10);
  554.         }
  555. }

  556. void LCD_list_Decimal(unsigned char x,unsigned char y,double Decimal)
  557. {
  558.         unsigned char pos;
  559.         double Deci;
  560.         unsigned long num;
  561.         num=(unsigned long)Decimal;
  562.         Deci=Decimal-num;
  563.         switch (x)
  564.         {
  565.                 case 1:
  566.                         x=0x7F;
  567.                         break;

  568.                 case 2:
  569.                         x=0x8F;
  570.                         break;

  571.                 case 3:
  572.                         x=0x87;
  573.                         break;

  574.                 case 4:
  575.                         x=0x97;
  576.                         break;
  577.         }
  578.         pos=x+y;
  579.         LCD_write_com(pos);
  580.         if(Decimal>=0 && Decimal<1)
  581.         {
  582.                 LCD_write_data(0x30+0);
  583.                 LCD_write_data('.');
  584.                 LCD_write_data(0x30+(int)(Deci*10)%10);
  585.                 LCD_write_data(0x30+(int)(Deci*100)%10);
  586.                 LCD_write_data(0x30+(int)(Deci*1000)%10);
  587.                 LCD_write_data(0x30+(int)(Deci*10000)%10);
  588.                 LCD_write_data(0x30+(int)(Deci*100000)%10);
  589.         }
  590.         if(Decimal>=1 && Decimal<10)
  591.         {
  592.                 LCD_write_data(0x30+num%10);
  593.                 LCD_write_data('.');
  594.                 LCD_write_data(0x30+(int)(Deci*10)%10);
  595.                 LCD_write_data(0x30+(int)(Deci*100)%10);
  596.                 LCD_write_data(0x30+(int)(Deci*1000)%10);
  597.                 LCD_write_data(0x30+(int)(Deci*10000)%10);
  598.                 LCD_write_data(0x30+(int)(Deci*100000)%10);
  599.         }
  600.         if(Decimal>=10 && Decimal<100)
  601.         {
  602.                 LCD_write_data(0x30+num/10);
  603.                 LCD_write_data(0x30+num%10);
  604.                 LCD_write_data('.');
  605.                 LCD_write_data(0x30+(int)(Deci*10)%10);
  606.                 LCD_write_data(0x30+(int)(Deci*100)%10);
  607.                 LCD_write_data(0x30+(int)(Deci*1000)%10);
  608.                 LCD_write_data(0x30+(int)(Deci*10000)%10);
  609.                 LCD_write_data(0x30+(int)(Deci*100000)%10);
  610.         }
  611.         if(Decimal>=100 && Decimal<1000)
  612.         {
  613.                 LCD_write_data(0x30+num/100);
  614.                 LCD_write_data(0x30+num/10%10);
  615.                 LCD_write_data(0x30+num%10);
  616.                 LCD_write_data('.');
  617.                 LCD_write_data(0x30+(int)(Deci*10)%10);
  618.                 LCD_write_data(0x30+(int)(Deci*100)%10);
  619.                 LCD_write_data(0x30+(int)(Deci*1000)%10);
  620.                 LCD_write_data(0x30+(int)(Deci*10000)%10);
  621.                 LCD_write_data(0x30+(int)(Deci*100000)%10);
  622.         }
  623.         if(Decimal>=1000 && Decimal<10000)
  624.         {
  625.                 LCD_write_data(0x30+num/1000);
  626.                 LCD_write_data(0x30+num/100%10);
  627.                 LCD_write_data(0x30+num/10%10);
  628.                 LCD_write_data(0x30+num%10);
  629.                 LCD_write_data('.');
  630.                 LCD_write_data(0x30+(int)(Deci*10)%10);
  631.                 LCD_write_data(0x30+(int)(Deci*100)%10);
  632.                 LCD_write_data(0x30+(int)(Deci*1000)%10);
  633.                 LCD_write_data(0x30+(int)(Deci*10000)%10);
  634.                 LCD_write_data(0x30+(int)(Deci*100000)%10);
  635.         }
  636. }

  637. void LCD_init(void) {
  638.         P1DIR |= BIT6 + BIT7;
  639.         _delay_cycles(2000);
  640.         LCD_write_com(0x30); //一次送8位数据
  641.         LCD_write_com(0x0C); //整体显示,游标off,游标位置off
  642.         LCD_write_com(0x01); //清DDRAM
  643.         LCD_write_com(0x02); //DDRAM地址归位
  644.         LCD_write_com(0x80); //设定DDRAM 7位地址000,0000到地址计数器AC
  645. }

  646. void LCD_clear(void){
  647.         LCD_write_com(0x01);
  648. }

  649. /*-----LCD12864绘图函数-------*/
  650. void GUI_clear()  //绘图清屏函数 (清屏指令在画图时不能使用)
  651. {
  652.         unsigned char i,j,k;
  653.         LCD_write_com(EXTEND_SET);//扩展指令集,8位数据传输,绘图开
  654.         LCD_write_com(DRAW_OFF);        //关闭绘图显示
  655.         for(i=0;i<2;i++)  //分上下两屏写
  656.         {
  657.                 for(j=0;j<32;j++)
  658.                 {
  659.                         LCD_write_com(0x80+j);        //写y坐标
  660.                         _delay_cycles(10);
  661.                         if(i==0)//写x坐标
  662.                         {
  663.                                 LCD_write_com(0x80);
  664.                                 _delay_cycles(100);
  665.                         }
  666.                         else
  667.                         {
  668.                                 LCD_write_com(0x88);
  669.                                 _delay_cycles(100);
  670.                         }
  671.                         for(k=0;k<16;k++)  //写一整行数据
  672.                         {
  673.                                 LCD_write_data(0x00);  //写高字节
  674.                                 LCD_write_data(0x00);  //写低字节
  675.                                 _delay_cycles(100);
  676.                         }
  677.                 }
  678.         }
  679.         LCD_write_com(DRAW_ON);        //打开绘图显示
  680.         LCD_write_com(BASIC_SET);//打开基本指令集
  681. }

  682. void GUI_draw_full_picture(const unsigned char *dat)//画满屏图片,横向取模
  683. {
  684.            unsigned char i,j,k;
  685.            unsigned char GDRAMAddrX=0x80;
  686.            unsigned char GDRAMAddrY=0x80;
  687.            for(i=0;i<2;i++)
  688.            {
  689.                            for(j=0;j<32;j++)
  690.                         {
  691.                                 for(k=0;k<8;k++)
  692.                                 {
  693.                                         LCD_write_com(DRAW_OFF);
  694.                                         LCD_write_com(GDRAMAddrY+j);
  695.                                         LCD_write_com(GDRAMAddrX+k);
  696.                                         LCD_write_data(*dat++);
  697.                                         LCD_write_data(*dat++);
  698.                                 }
  699.                         }
  700.                         GDRAMAddrX=0x88;
  701.            }
  702.            LCD_write_com(DRAW_ON);
  703.            LCD_write_com(BASIC_SET);
  704. }

  705. /*
  706. void LCD_set_dot(unsigned char x,unsigned char y)//打点程序
  707. {
  708.          unsigned char x_byte,x_bit;  //确定在坐标的那一字节哪一位
  709.          unsigned char y_byte,y_bit;  //确定在坐标的哪一屏哪一行
  710.          unsigned char temph,templ;         //定义两个临时变量,用于存放读出来的数据
  711.          LCD_write_com(EXTEND_SET);        //扩展指令集
  712.          LCD_write_com(DRAW_OFF);//绘图显示关闭
  713.          x_byte=x/16;
  714.          x_bit=x%16;
  715.          y_byte=y/32;
  716.          y_bit=y%32;
  717.          LCD_write_com(EXTEND_SET);
  718.          LCD_write_com(0x80+y_bit);
  719.          LCD_write_com(0x80+x_byte+8*y_byte);
  720.          LCD_read_byte();
  721.          temph=LCD_read_byte();
  722.          templ=LCD_read_byte();
  723.          _delay_cycles(10);
  724.          LCD_write_com(0x80+y_bit);
  725.          LCD_write_com(0x80+x_byte+8*y_byte);
  726.          _delay_cycles(10);
  727.          if(x_bit<8)
  728.          {
  729.                  LCD_write_data(temph|(0x01<<(7-x_bit)));
  730.                 LCD_write_data(templ);
  731.          }
  732.          else
  733.          {
  734.                  LCD_write_data(temph);
  735.                 LCD_write_data(templ|(0x01<<(15-x_bit)));
  736.          }
  737.          LCD_write_com(DRAW_ON);
  738.          LCD_write_com(BASIC_SET);
  739. }
  740. */
复制代码


Sound_positioning_system1_0.rar

39.16 KB, 下载次数: 10, 下载积分: 黑币 -5

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表