Last active
April 29, 2018 23:25
-
-
Save xiupos/d74930f41ea1c25548fe041289cab217 to your computer and use it in GitHub Desktop.
PIC18F14K50 Blink(2)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Timer0(16bit!)のテスト | |
// ha2zakura | |
#include <xc.h> // PIC のハードウエア定義 | |
#define _XTAL_FREQ 48000000 | |
#define true 1 | |
#define false 0 | |
#define int8_t signed char | |
#define int16_t signed short | |
#define int32_t signed int | |
#define int64_t signed long long | |
#define uint8_t unsigned char | |
#define uint16_t unsigned short | |
#define uint32_t unsigned int | |
#define uint64_t unsigned long long | |
#pragma config FOSC = HS, PLLEN = ON, FCMEN = OFF | |
#pragma config IESO = OFF, USBDIV = OFF, CPUDIV = NOCLKDIV | |
#pragma config PWRTEN = OFF, BOREN = OFF, WDTEN = OFF | |
#pragma config HFOFST = OFF, MCLRE = ON | |
#pragma config STVREN = ON, BBSIZ = OFF, LVP = OFF | |
#pragma config XINST = OFF | |
#pragma config CP0 = OFF, CP1 = OFF, CPB = OFF | |
#pragma config WRT0 = OFF, WRT1 = OFF, WRTB = OFF, WRTC = OFF | |
#pragma config EBTR0 = OFF, EBTR1 = OFF, EBTRB = OFF | |
uint16_t tmr0v; | |
float tmr0f; | |
void clock_init(void) { | |
// 外部クロック(12MHz)を4xPLL | |
// config FOSC = HS, PLLEN = ON | |
OSCCON = 0b00000000; | |
OSCCON2 |= (1 << 2); // PRI_SD = 1 | |
} | |
void tmr0_init(uint16_t tmr0, uint8_t p) { | |
T0CON = 0b10000000 | p; | |
tmr0v = tmr0; | |
TMR0H = tmr0v >> 8; | |
TMR0L = tmr0v & 0xFF; | |
T0IF = 0; | |
T0IE = 1; | |
GIE = 1; | |
} | |
void interrupt ISR(void) { | |
if(T0IF == 1) { | |
tmr0f = true; | |
TMR0H = tmr0v >> 8; | |
TMR0L = tmr0v & 0x00FF; | |
T0IF = 0; | |
} | |
} | |
void delay(uint32_t ms) { | |
do { | |
__delay_us(1000); | |
} while(--ms > 0); | |
} | |
void main(void) { | |
clock_init(); | |
tmr0_init(56161, 0b111); | |
TRISA |= 1 << 3; | |
TRISB &= ~(1 << 7); | |
LATB = 0; | |
while(1){ | |
if(tmr0f){ | |
LATB |= 1 << 7; | |
delay(100); | |
LATB &= ~(1 << 7); | |
tmr0f = false; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Timer1のテスト | |
// ha2zakura | |
#include <xc.h> // PIC のハードウエア定義 | |
#define _XTAL_FREQ 48000000 | |
#define true 1 | |
#define false 0 | |
#define int8_t signed char | |
#define int16_t signed short | |
#define int32_t signed int | |
#define int64_t signed long long | |
#define uint8_t unsigned char | |
#define uint16_t unsigned short | |
#define uint32_t unsigned int | |
#define uint64_t unsigned long long | |
#pragma config FOSC = HS, PLLEN = ON, FCMEN = OFF | |
#pragma config IESO = OFF, USBDIV = OFF, CPUDIV = NOCLKDIV | |
#pragma config PWRTEN = OFF, BOREN = OFF, WDTEN = OFF | |
#pragma config HFOFST = OFF, MCLRE = ON | |
#pragma config STVREN = ON, BBSIZ = OFF, LVP = OFF | |
#pragma config XINST = OFF | |
#pragma config CP0 = OFF, CP1 = OFF, CPB = OFF | |
#pragma config WRT0 = OFF, WRT1 = OFF, WRTB = OFF, WRTC = OFF | |
#pragma config EBTR0 = OFF, EBTR1 = OFF, EBTRB = OFF | |
uint16_t tmr1v; | |
float tmr1f; | |
void clock_init(void) { | |
// 外部クロック(12MHz)を4xPLL | |
// config FOSC = HS, PLLEN = ON | |
OSCCON = 0b00000000; | |
OSCCON2 |= (1 << 2); // PRI_SD = 1 | |
} | |
void tmr1_init(uint16_t tmr1, uint8_t p) { | |
T1CON = 0b10000001 | (p << 4); | |
tmr1v = tmr1; | |
TMR1H = tmr1v >> 8; | |
TMR1L = tmr1v & 0xFF; | |
TMR1IF = 0; | |
TMR1IE = 1; | |
PEIE = 1; | |
GIE = 1; | |
} | |
void interrupt ISR(void) { | |
if(TMR1IF == 1) { | |
tmr1f = true; | |
TMR1H = tmr1v >> 8; | |
TMR1L = tmr1v & 0x00FF; | |
TMR1IF = 0; | |
} | |
} | |
void delay(uint32_t ms) { | |
do { | |
__delay_us(1000); | |
} while(--ms > 0); | |
} | |
void main(void) { | |
clock_init(); | |
tmr1_init(59536, 0b11); | |
TRISA |= 1 << 3; | |
TRISB &= ~(1 << 7); | |
LATB = 0; | |
while(1){ | |
if(tmr1f){ | |
LATB |= 1 << 7; | |
delay(1); | |
LATB &= ~(1 << 7); | |
tmr1f = false; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Timer2のテスト | |
// ha2zakura | |
#include <xc.h> // PIC のハードウエア定義 | |
#define _XTAL_FREQ 48000000 | |
#define true 1 | |
#define false 0 | |
#define int8_t signed char | |
#define int16_t signed short | |
#define int32_t signed int | |
#define int64_t signed long long | |
#define uint8_t unsigned char | |
#define uint16_t unsigned short | |
#define uint32_t unsigned int | |
#define uint64_t unsigned long long | |
#pragma config FOSC = HS, PLLEN = ON, FCMEN = OFF | |
#pragma config IESO = OFF, USBDIV = OFF, CPUDIV = NOCLKDIV | |
#pragma config PWRTEN = OFF, BOREN = OFF, WDTEN = OFF | |
#pragma config HFOFST = OFF, MCLRE = ON | |
#pragma config STVREN = ON, BBSIZ = OFF, LVP = OFF | |
#pragma config XINST = OFF | |
#pragma config CP0 = OFF, CP1 = OFF, CPB = OFF | |
#pragma config WRT0 = OFF, WRT1 = OFF, WRTB = OFF, WRTC = OFF | |
#pragma config EBTR0 = OFF, EBTR1 = OFF, EBTRB = OFF | |
uint8_t tmr2v; | |
float tmr2f; | |
void clock_init(void) { | |
// 外部クロック(12MHz)を4xPLL | |
// config FOSC = HS, PLLEN = ON | |
OSCCON = 0b00000000; | |
OSCCON2 |= (1 << 2); // PRI_SD = 1 | |
} | |
void tmr2_init(uint8_t tmr2, uint8_t pre, uint8_t post) { | |
T2CON = 0b10000100 | pre | (post << 3); | |
tmr2v = tmr2; | |
PR2 = tmr2v; | |
TMR2 = 0; | |
TMR2IF = 0; | |
TMR2IE = 1; | |
PEIE = 1; | |
GIE = 1; | |
} | |
void interrupt ISR(void) { | |
if(TMR2IF == 1) { | |
tmr2f = true; | |
TMR2IF = 0; | |
} | |
} | |
void delay(uint32_t ms) { | |
do { | |
__delay_us(1000); | |
} while(--ms > 0); | |
} | |
void main(void) { | |
clock_init(); | |
tmr2_init(250, 0b10, 12); | |
TRISA |= 1 << 3; | |
TRISB &= ~(1 << 7); | |
LATB = 0; | |
while(1){ | |
if(tmr2f){ | |
LATB |= 1 << 7; | |
delay(1); | |
LATB &= ~(1 << 7); | |
tmr2f = false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment