Advertisement
a5768549

ATmega8_p3

Jul 4th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.22 KB | None | 0 0
  1. //ICC-AVR application builder
  2. //atmega8_p3
  3. /*
  4.     功能:
  5.         預設在七段顯示器顯示ADC7可變電阻值
  6.         按鈕按第一下:七段顯示器從ADC7值跑到0
  7.         按鈕按第二下:七段顯示器從0跑到ADC7值,同時記錄回到第一下
  8. */
  9. #include<iom8v.h>
  10. #include<macros.h>
  11.  
  12. #define S_1      PORTC&=~BIT(0);PORTC|=BIT(1);PORTC|=BIT(2);PORTC|=BIT(3);
  13. #define S_2      PORTC|=BIT(0);PORTC&=~BIT(1);PORTC|=BIT(2);PORTC|=BIT(3);
  14. #define S_3      PORTC|=BIT(0);PORTC|=BIT(1);PORTC&=~BIT(2);PORTC|=BIT(3);  
  15. #define S_4      PORTC|=BIT(0);PORTC|=BIT(1);PORTC|=BIT(2);PORTC&=~BIT(3);
  16. #define S_OFF    PORTC|=BIT(0);PORTC|=BIT(1);PORTC|=BIT(2);PORTC|=BIT(3);  
  17. #define S_ON     PORTC&=~BIT(0);PORTC&=~BIT(1);PORTC&=~BIT(2);PORTC&=~BIT(3);
  18.  
  19. unsigned char tab[16] =
  20. {
  21.     0x3f,0x06,0x5b,0x4f,
  22.     0x66,0x6d,0x7d,0x07,
  23.     0x7f,0x6f,0x77,0x7c,
  24.     0x39,0x5e,0x79,0x71
  25. };
  26.                          
  27. //初始化 變量 聲明//////////////////////////////////////////////////////////////
  28. unsigned char cnn = 0;//定時器變量
  29. unsigned char i   = 0;//數碼管位選用
  30. unsigned int  j   = 0;
  31. unsigned char ad  = 0;//AD採集間隔
  32. unsigned int ad_data2=0;
  33. int count = 0;
  34. int flag  = 0;
  35. int th  = 0;
  36. int hu  = 0;
  37. int ten = 0;
  38. int num = 0;
  39.  
  40. void de_led(void)
  41. {
  42.     S_OFF;
  43.     th = ad_data2 / 1000;
  44.     PORTD = tab[th];
  45.     S_1;
  46.     delay_ms(1);
  47.  
  48.     S_OFF;
  49.     hu = (ad_data2 / 100) % 10;
  50.     PORTD = tab[hu]|0x80;
  51.     S_2;
  52.     delay_ms(1);
  53.  
  54.     S_OFF;
  55.     ten = (ad_data2 / 10) % 10;
  56.     PORTD = tab[ten];
  57.     S_3;
  58.     delay_ms(1);
  59.  
  60.     S_OFF;
  61.     num = ad_data2 % 10;
  62.     PORTD = tab[num];
  63.     S_4;
  64.     delay_ms(1);
  65. }
  66. void btn1(void)
  67. {
  68.     flag = ad_chane(ad_cat());
  69.     for(j = flag;j>0;j--)
  70.     {
  71.         S_OFF;
  72.         th = j / 1000;
  73.         PORTD = tab[th];
  74.         S_1;
  75.         delay_ms(2);
  76.  
  77.         S_OFF;
  78.         hu = (j / 100) % 10;
  79.         PORTD = tab[hu]|0x80;
  80.         S_2;
  81.         delay_ms(2);
  82.  
  83.         S_OFF;
  84.         ten = (j / 10) % 10;
  85.         PORTD = tab[ten];
  86.         S_3;
  87.         delay_ms(2);
  88.  
  89.         S_OFF;
  90.         num = j % 10;
  91.         PORTD = tab[num];
  92.         S_4;
  93.         delay_ms(1);
  94.     }
  95. }
  96.  
  97. void btn2(void)
  98. {
  99.     flag = ad_chane(ad_cat());
  100.     for(j = 0;j<flag;j++)
  101.     {
  102.         S_OFF;
  103.         th = j / 1000;
  104.         PORTD = tab[th];
  105.         S_1;
  106.         delay_ms(2);
  107.         S_OFF;
  108.         hu = (j / 100) % 10;
  109.         PORTD = tab[hu]|0x80;
  110.         S_2;
  111.         delay_ms(2);
  112.         S_OFF;
  113.         ten = (j / 10) % 10;
  114.         PORTD = tab[ten];
  115.         S_3;
  116.         delay_ms(2);
  117.         S_OFF;
  118.         num = j % 10;
  119.         PORTD = tab[num];
  120.         S_4;
  121.         delay_ms(1);
  122.     }
  123. }
  124.  
  125. void key_scan(void)
  126. {
  127.     delay_ms(20);
  128.     if((PINB&0x3F)!=0x3F)     //假如1~8顆按鈕有被按下去(=0)
  129.     {
  130.         delay_ms(20);
  131.         if((PINB&0X3B) == 0X3B)//0X3B = S2按鈕
  132.         {
  133.             delay_ms(20);
  134.             if(count == 0)
  135.             {
  136.                 btn1();
  137.                 count = count + 1;
  138.                 delay_ms(100);
  139.             }
  140.             else if(count == 1)
  141.             {
  142.                 btn2();
  143.                 count = 0;
  144.                 delay_ms(100);
  145.             }
  146.             //LED = 1 會等於 LED = 0XF1,LED = 2 會等於 LED = 0XF2,以此類推
  147.         }
  148.     }
  149. }
  150. /*******************************************************************************
  151. * 函數名稱: delay_us()
  152. * 入口參數: microsecond : 輸入延時微秒的時間
  153. * 出口參數:
  154. * 功能描述: 微秒的延時        
  155. *******************************************************************************/
  156. void delay_us(unsigned int microsecond)
  157. {      
  158.     do
  159.     {
  160.         microsecond--;
  161.     }        
  162.     while (microsecond>1);
  163. }
  164. /*******************************************************************************
  165. * 函數名稱: delay_ms()
  166. * 入口參數: millisecond : 輸入延時毫秒的時間
  167. * 出口參數:
  168. * 功能描述: 毫秒的延時    
  169. *******************************************************************************/
  170. void delay_ms(unsigned int millisecond)
  171. {      
  172.     while (millisecond--)
  173.     {
  174.         delay_us(999);
  175.     }  
  176. }
  177.  
  178. //I、O口初始化
  179. void por_init(void)
  180. {
  181.    DDRB  = 0XC0;//1100 0000
  182.    PORTB = 0XFF;
  183.    PORTD = 0XFF;
  184.    DDRD = 0XFF;
  185.    PORTC = 0X0F;//AD採集口 ADC 設置為輸入
  186.    DDRC = 0X0F;
  187.    
  188. }
  189.  
  190. void display(unsigned char n)
  191. {
  192.     PORTD = tab[n];
  193. }
  194.  
  195. void display1(unsigned char n)
  196. {
  197.     PORTD = tab[n]|0X80;
  198. }
  199.  
  200. //定時器0初始化
  201. void timer0_int(void)
  202. {
  203.     TCCR0 =(1<<CS01) | (1<<CS00);
  204.     TCNT0 = 0X05;
  205.     TIMSK = 0x01; //T0/C0溢出中斷使能
  206. }
  207.  
  208. //CPU總體初始化
  209. void init_devices(void)
  210. {
  211.    por_init();
  212.    timer0_int();
  213.    SEI();
  214. }
  215.  
  216. unsigned int ad_cat(void)//電壓採集函數
  217. {
  218.     unsigned int t1,t2;
  219.     ADCSRA = 0X00;//disable adc
  220.     ADMUX=0x07;//ref  左對齊  ADC7
  221.     ACSR = 0x80;//使能adc可用,不用修改
  222.     ADCSRA|=BIT(ADEN);          
  223.     ADCSRA|=BIT(ADSC);          
  224.     while(!(ADCSRA&(BIT(ADIF))));
  225.     ADCSRA&=~BIT(ADIF);
  226.     t1 = (unsigned int)ADCL;
  227.     t2 = (unsigned int)ADCH;
  228.     t2 = (t2<<8)+t1;
  229.     return t2;
  230. }
  231.  
  232. unsigned int ad_chane(unsigned int i)//電壓轉換函數
  233. {
  234.    long x;
  235.    unsigned int y;
  236.    x = (2500*(long)i)/1024;
  237.    y = (unsigned int)x;
  238.    return y;
  239. }
  240.  
  241. //定時器0///////////////////////////////////////////////////////////////////////
  242. #pragma interrupt_handler timer0_ovf_isr:10
  243. void timer0_ovf_isr(void)
  244. {    
  245.    TCNT0 = 0X05;//內部晶振,幾分頻算出時間初值
  246.    ad++;
  247.    if(50==ad)//AD採集
  248.    {
  249.         ad=0;
  250.         ad_data2=ad_chane(ad_cat());
  251.    }
  252. }//end of void timer0_ovf_isr(void)
  253.  
  254. void main(void)
  255. {
  256.    init_devices();
  257.    while(1)
  258.    {
  259.         key_scan();
  260.         de_led();
  261.    }
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement