pic16f648a_xc8_led_blink_06\main.c
  1 #include <xc.h>
  2 #include <string.h>
  3 
  4 #define _XTAL_FREQ 10000000     // 10MHz
  5 // CONFIG
  6 #pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator: High-speed crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN)
  7 #pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
  8 #pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
  9 #pragma config MCLRE = OFF      // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is MCLR)
 10 #pragma config BOREN = ON       // Brown-out Detect Enable bit (BOD enabled)
 11 #pragma config LVP = OFF        // Low-Voltage Programming Enable bit (RB4/PGM pin has digital I/O function, HV on MCLR must be used for programming)
 12 #pragma config CPD = OFF        // Data EE Memory Code Protection bit (Data memory code protection off)
 13 #pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)
 14 
 15 void func_delay_ms(unsigned int);
 16 unsigned int delayvalue(unsigned int);
 17 void memc1(void);
 18 void memc2(void);
 19 void memc3(void);
 20 void memc4(void);
 21 
 22 unsigned char patternA[12];
 23 unsigned char patternB[12];
 24 
 25 unsigned char patternB2[12] = {
 26         0b00000001, 0b00000010, 0b00000100, 0b00001000, 0b00010000,
 27         0b00100000, 0b01000000, 0b10000000, 0b00000000, 0b00000000,
 28         0b00000000, 0b00000000
 29     };
 30     unsigned char patternA2[12] = {
 31         0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
 32         0b00000000, 0b00000000, 0b00000000, 0b00000001, 0b00000010,
 33         0b00000100, 0b00001000
 34     };
 35     unsigned char patternB1[12] = {
 36         0b10101010, 0b01010101, 0b10101010, 0b01010101, 0b10101010,
 37         0b01010101, 0b10101010, 0b01010101, 0b10101010, 0b01010101,
 38         0b10101010, 0b01010101
 39     };
 40     unsigned char patternA1[12] = {
 41         0b00001010, 0b00000101, 0b00001010, 0b00000101, 0b00001010,
 42         0b00000101, 0b00001010, 0b00000101, 0b00001010, 0b00000101,
 43         0b00001010, 0b00000101
 44     };
 45     unsigned char patternB3[12] = {
 46         0b00000001, 0b00000010, 0b00000100, 0b00001000, 0b10010000,
 47         0b01100000, 0b10010000, 0b00001000, 0b00000100, 0b00000010,
 48         0b00000001, 0b00000000
 49     };
 50     unsigned char patternA3[12] = {
 51         0b00001000, 0b00000100, 0b00000010, 0b00000001, 0b00000000,
 52         0b00000000, 0b00000000, 0b00000001, 0b00000010, 0b00000100,
 53         0b00001000, 0b00000000
 54     };
 55     unsigned char patternB4[12] = {
 56         0b11111111, 0b00000000, 0b11111111, 0b00000000, 0b11111111,
 57         0b00000000, 0b11111111, 0b00000000, 0b11111111, 0b00000000,
 58         0b11111111, 0b00000000
 59     };
 60     unsigned char patternA4[12] = {
 61         0b00001111, 0b00000000, 0b00001111, 0b00000000, 0b00001111,
 62         0b00000000, 0b00001111, 0b00000000, 0b00001111, 0b00000000,
 63         0b00001111, 0b00000000
 64     };
 65 
 66 unsigned int dv = 500;
 67 unsigned int fg = 1;
 68 
 69 
 70 void main(){
 71     unsigned char i;
 72 
 73     memcpy(patternA, patternA1,sizeof(patternA));
 74     memcpy(patternB, patternB1,sizeof(patternB));
 75 
 76     CMCON = 0x00000111;
 77     TRISA = 0b00010000;
 78     TRISB = 0b00000000;
 79     
 80     while(1){
 81 
 82         for(i = 0; i <= 11; i++){
 83             fg = delayvalue(fg);
 84             PORTA = patternA[i];
 85             PORTB = patternB[i];
 86             func_delay_ms(dv);
 87         }
 88         for(i = 10; i >= 1; i--){
 89             fg = delayvalue(fg);
 90             PORTA = patternA[i];
 91             PORTB = patternB[i];
 92             func_delay_ms(dv);
 93         }
 94     }
 95 }
 96 
 97 void func_delay_ms(unsigned int milliseconds) {
 98    while(milliseconds > 0)
 99    {
100        __delay_ms(1);
101       milliseconds--;
102     }
103  }
104 
105 unsigned int delayvalue(unsigned int fg){
106     if( RA4 == 0 ){
107         switch ( fg ){
108             case 1:
109                 memc1();
110                 dv = 500;
111                 fg = 2;
112                 break;
113             case 2:
114                 memc1();
115                 dv = 100;
116                 fg = 3;
117                 break;
118             case 3:
119                 memc1();
120                 dv = 50;
121                 fg = 4;
122                 break;
123             case 4:
124                 memc3();
125                 dv = 500;
126                 fg = 5;
127                 break;
128             case 5:
129                 memc3();
130                 dv = 100;
131                 fg = 6;
132                 break;
133             case 6:
134                 memc3();
135                 dv = 50;
136                 fg = 7;
137                 break;
138             case 7:
139                 memc2();
140                 dv = 500;
141                 fg = 8;
142                 break;
143             case 8:
144                 memc2();
145                 dv = 100;
146                 fg = 9;
147                 break;
148             case 9:
149                 memc2();
150                 dv = 50;
151                 fg = 10;
152                 break;
153             case 10:
154                 memc2();
155                 dv = 20;
156                 fg = 11;
157                 break;
158             case 11:
159                 memc2();
160                 dv = 10;
161                 fg = 12;
162                 break;
163             case 12:
164                 memc4();
165                 dv = 100;
166                 fg = 1;
167                 break;
168         }
169         while( RA4==0 ){
170         }
171     }
172     return fg;
173 }
174 
175 void memc1(void){
176     memcpy(patternA, patternA1,sizeof(patternA));
177     memcpy(patternB, patternB1,sizeof(patternB));
178 }
179 
180 void memc2(void){
181     memcpy(patternA, patternA2,sizeof(patternA));
182     memcpy(patternB, patternB2,sizeof(patternB));
183 }
184 
185 void memc3(void){
186     memcpy(patternA, patternA3,sizeof(patternA));
187     memcpy(patternB, patternB3,sizeof(patternB));
188 }
189 
190 void memc4(void){
191     memcpy(patternA, patternA4,sizeof(patternA));
192     memcpy(patternB, patternB4,sizeof(patternB));
193 }
194