PIC16F1827をブレッドボード上で、基本回路を組んでみました。
main.c
// PIC16F1827 Configuration Bit Settings
// 'C' source line config statements
// CONFIG1
//#pragma config FOSC = HS // Oscillator Selection (HS Oscillator, High-speed crystal/resonator connected between OSC1 and OSC2 pins)
#pragma config FOSC = INTOSC // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
#pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
#pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled)
#pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled)
#pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config IESO = ON // Internal/External Switchover (Internal/External Switchover mode is enabled)
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)
// CONFIG2
#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
#pragma config PLLEN = ON // PLL Enable (4x PLL enabled)
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LVP =ON // Low-Voltage Programming Enable (Low-voltage programming enabled)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#include <stdlib.h>
#include <stdio.h>
#include "Interrupt.h"
#include "Peripheral.h"
#define _XTAL_FREQ 16000000
void Oscillator_Init(void);
void Port_Init(void);
void main(void)
{
//Basic Hard Initialize
Oscillator_Init();
Port_Init();
//Peripheral
Timer0_INIT();
//割り込み許可
Interrupt_START();
while(1)
{
//Timer0 interrupt processing
if(tm0.up)
{
tm0.up=false;
LATBbits.LATB0=~LATBbits.LATB0;
INTCONbits.TMR0IE=1;
}
}
return;
}
void Oscillator_Init(void)
{
OSCCONbits.SPLLEN=0;
OSCCONbits.IRCF=0b1111;//16Mhz
OSCCONbits.SCS=0b10;//InternalOscillator
}
void Port_Init(void)
{
TRISA=0x00;
ANSELA=0x00;
TRISB=0x00; //RX:RB1
ANSELB=0x00;
PORTA=0x00;
PORTB=0x00;
}
Interrupt.h
#ifndef INTERRUPT_H
#define INTERRUPT_H
#ifdef __cplusplus
extern "C" {
#endif
extern void __interrupt()isr();
#ifdef __cplusplus
}
#endif
#endif /* INTERRUPT_H */
Interrupt.c
#include <pic16f1827.h>
#include "Peripheral.h"
#include "Interrupt.h"
void __interrupt()isr()
{
//Timer0割り込み処理
if(INTCONbits.TMR0IE==1 && INTCONbits.TMR0IF )
{
INTCONbits.TMR0IF=0;
tm0.cnt++;
if(tm0.cnt==50)
{
tm0.cnt=0;
INTCONbits.TMR0IE=0;
tm0.up=true;
}
TMR0=0x63;
}
}
Peripheral.h
#ifndef PERIPHERAL_H
#define PERIPHERAL_H
#include <xc.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define _XTAL_FREQ 16000000
#ifdef __cplusplus
extern "C" {
#endif
//************************************************//
//Timer0 8bits timer
//************************************************//
#define T0_UP 1
#define T0_STOP 0
typedef struct{
bool up;
uint16_t cnt;
}_tm0;
extern _tm0 tm0;
extern void Timer0_INIT(void);
extern void Interrupt_START(void);
#ifdef __cplusplus
}
#endif
#endif
Peripheral.c
#include <pic16f1827.h>
#include "Peripheral.h"
/*---------------------------------------------------
Timer0 8bits Timer
---------------------------------------------------*/
_tm0 tm0;
void Timer0_INIT(void)
{
OPTION_REGbits.TMR0CS = 0; //ClockSource:Fosc/4
OPTION_REGbits.PSA = 0; //assingned to the Timer0
OPTION_REGbits.PS = 0b111; //1:256
TMR0=0x63; //Fosc:32Mhz interval time 10ms
INTCONbits.T0IE = 1;
tm0.cnt=0;
}
//interrupt start
void Interrupt_START(void)
{
INTCONbits.PEIE=1;
INTCONbits.GIE=1;
}