LEDを3つつないでLチカを2種類やってみた。
1つはPWMっぽく弱強弱の点滅をするもの。
もう1つは単純な点滅だ。
PWMもどきの方は拡大するとこんな感じだ。
PWMっぽい点滅のコード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
#include <xc.h> #include <stdlib.h> #include <pic10F222.h> // CONFIG #pragma config IOSCFS = 8MHZ // Internal Oscillator Frequency Select bit (8 MHz) #pragma config MCPU = ON // Master Clear Pull-up Enable bit (Pull-up enabled) #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled) #pragma config CP = OFF // Code protection bit (Code protection off) #pragma config MCLRE = OFF // GP3/MCLR Pin Function Select bit (GP3/MCLR pin function is digital I/O, MCLR internally tied to VDD) #define _XTAL_FREQ 8000000 void DelayPart(unsigned char delayc); void OnOff0(unsigned char i, unsigned char j); void OnOff1(unsigned char i, unsigned char j); void OnOff2(unsigned char i, unsigned char j); int main(void) { ADCON0bits.ANS0 = 0;//GP0/AN0 configured as digital I/O ADCON0bits.ANS1 = 0;//GP1/AN1 configured as digital I/O OSCCALbits.FOSC4 = 0;//GP2/T0CKI applied to GP2 OPTION = 0b00000000;//Timer0 Clock Source is not T0CKI TRISGPIO = 0b00000000;//All for Output unsigned char i; unsigned char j = 5; unsigned char k = 10; while(1){ for(i=0;i<240;i=i+k){ OnOff0(i, j); } for(i=240;i>0;i=i-k){ OnOff0(i, j); } GP0 = 0; for(i=0;i<250;i=i+k){ OnOff1(i, j); } for(i=240;i>0;i=i-k){ OnOff1(i, j); } GP1 = 0; for(i=0;i<250;i=i+k){ OnOff2(i, j); } for(i=240;i>0;i=i-k){ OnOff2(i, j); } GP2 = 0; for(i=0;i<250;i=i+k){ OnOff1(i, j); } for(i=240;i>0;i=i-k){ OnOff1(i, j); } GP1 = 0; } } void DelayPart(unsigned char delayc){ while(delayc > 0){ delayc--; __delay_us(5); } } void OnOff0(unsigned char i, unsigned char j){ while(j>0){ j--; GP0 = 0; DelayPart(240-i); GP0 = 1; DelayPart(i); } } void OnOff1(unsigned char i, unsigned char j){ while(j>0){ j--; GP1 = 0; DelayPart(240-i); GP1 = 1; DelayPart(i); } } void OnOff2(unsigned char i, unsigned char j){ while(j>0){ j--; GP2 = 0; DelayPart(240-i); GP2 = 1; DelayPart(i); } } |
単純な点滅のコード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#include <xc.h> #include <stdlib.h> #include <pic10F222.h> // CONFIG #pragma config IOSCFS = 8MHZ // Internal Oscillator Frequency Select bit (8 MHz) #pragma config MCPU = ON // Master Clear Pull-up Enable bit (Pull-up enabled) #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled) #pragma config CP = OFF // Code protection bit (Code protection off) #pragma config MCLRE = OFF // GP3/MCLR Pin Function Select bit (GP3/MCLR pin function is digital I/O, MCLR internally tied to VDD) #define _XTAL_FREQ 8000000 void delay_ms(unsigned char delayc); void OnOff0(unsigned char i, unsigned char j); void OnOff1(unsigned char i, unsigned char j); void OnOff2(unsigned char i, unsigned char j); int main(void) { ADCON0bits.ANS0 = 0;//GP0/AN0 configured as digital I/O ADCON0bits.ANS1 = 0;//GP1/AN1 configured as digital I/O OSCCALbits.FOSC4 = 0;//GP2/T0CKI applied to GP2 OPTION = 0b00000000;//Timer0 Clock Source is not T0CKI TRISGPIO = 0b00000000;//All for Output while(1){ GP0 = 1; __delay_ms(700); GP0 = 0; GP1 = 1; __delay_ms(700); GP1 = 0; GP2 = 1; __delay_ms(700); GP2 = 0; GP1 = 1; __delay_ms(700); GP1 = 0; } } |
Tweet