pic16f84a_xc8_led_blink_08\main.c
 1 #include <xc.h>
 2 #define _XTAL_FREQ 10000000     // 10MHz
 3 #pragma config FOSC = HS, WDTE = OFF, PWRTE = OFF, CP = OFF
 4 
 5 void func_delay_ms(unsigned int);
 6 unsigned int delayvalue(unsigned int);
 7 
 8 void main()
 9 {
10     unsigned int d = 500;
11     unsigned char i;
12     unsigned char patternB[12] = {
13         0b00000001,
14         0b00000010,
15         0b00000100,
16         0b00001000,
17         0b00010000,
18         0b00100000,
19         0b01000000,
20         0b10000000,
21         0b00000000,
22         0b00000000,
23         0b00000000,
24         0b00000000,
25     };
26     unsigned char patternA[12] = {
27         0b00000000,
28         0b00000000,
29         0b00000000,
30         0b00000000,
31         0b00000000,
32         0b00000000,
33         0b00000000,
34         0b00000000,
35         0b00000001,
36         0b00000010,
37         0b00000100,
38         0b00001000,
39     };
40 
41     TRISA = 0b00010000;
42     TRISB = 0b00000000;
43     PORTA = 0x00;
44     PORTB = 0x00;
45 
46     while(1){
47 
48         for(i = 0; i <= 11; i++){
49             d = delayvalue(d);
50             PORTA = patternA[i];
51             PORTB = patternB[i];
52             func_delay_ms(d);
53         }
54         for(i = 10; i >= 1; i--){
55             d = delayvalue(d);
56             PORTA = patternA[i];
57             PORTB = patternB[i];
58             func_delay_ms(d);
59         }
60     }
61 }
62 
63 void func_delay_ms(unsigned int milliseconds) {
64    while(milliseconds > 0)
65    {
66        __delay_ms(1);
67       milliseconds--;
68     }
69  }
70 
71 unsigned int delayvalue(unsigned int dv){
72     if( RA4 == 0 ){
73         switch ( dv ){
74             case 10: dv = 500;
75             break;
76             case 500: dv = 100;
77             break;
78             case 100: dv = 50;
79             break;
80             case 50: dv = 10;
81             break;
82         }
83         while( RA4==0 ){
84         }
85     }
86     return dv;
87 }
88