Hello friends,
Few years ago when I thought I had time for MCU I bought PIC18f1320 and kind off forgot about it. Now after stumbling on to EEVBlog chanel I decided to give it a try.
After poking around with setting port pins I came to idea to control RGB led brightness and color using PWM. SInce 18f1320 has only one PWM output and I need 3 of them I decided to use a interrupt on timer0 to try to control diodes.
SInce I never used timer0 interrupts I decided to make a simple test which is going to turn off and on diode on PORTA RA0 (pin1, 18 pin PDIP package) on my MCU.
Its a simple setup just a diode from pin 1 to R and then to GND. Internal oscillator set to 8MHz
So I made a code (MPLAB IDE, XC8) that goes something like this
/*
* File: TImer0.c
* Author: Asus
*
* Created on 13.11.2018., 00.09
*/
// PIC18F1320 Configuration Bit Settings
// 'C' source line config statements
// CONFIG1H
#pragma config OSC = INTIO2 // Oscillator Selection bits (Internal RC oscillator, port function on RA6 and port function on RA7)
#pragma config FSCM = ON // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor enabled)
#pragma config IESO = ON // Internal External Switchover bit (Internal External Switchover mode enabled)
// CONFIG2L
#pragma config PWRT = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOR = ON // Brown-out Reset Enable bit (Brown-out Reset enabled)
// BORV = No Setting
// CONFIG2H
#pragma config WDT = ON // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
#pragma config WDTPS = 32768 // Watchdog Timer Postscale Select bits (1:32768)
// CONFIG3H
#pragma config MCLRE = OFF // MCLR Pin Enable bit (RA5 input pin enabled, MCLR disabled)
// CONFIG4L
#pragma config STVR = ON // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config LVP = ON // Low-Voltage ICSP Enable bit (Low-Voltage ICSP enabled)
// CONFIG5L
#pragma config CP0 = OFF // Code Protection bit (Block 0 (00200-000FFFh) not code-protected)
#pragma config CP1 = OFF // Code Protection bit (Block 1 (001000-001FFFh) not code-protected)
// CONFIG5H
#pragma config CPB = OFF // Boot Block Code Protection bit (Boot Block (000000-0001FFh) not code-protected)
#pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM not code-protected)
// CONFIG6L
#pragma config WRT0 = OFF // Write Protection bit (Block 0 (00200-000FFFh) not write-protected)
#pragma config WRT1 = OFF // Write Protection bit (Block 1 (001000-001FFFh) not write-protected)
// CONFIG6H
#pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)
#pragma config WRTB = OFF // Boot Block Write Protection bit (Boot Block (000000-0001FFh) not write-protected)
#pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM not write-protected)
// CONFIG7L
#pragma config EBTR0 = OFF // Table Read Protection bit (Block 0 (00200-000FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF // Table Read Protection bit (Block 1 (001000-001FFFh) not protected from table reads executed in other blocks)
// CONFIG7H
#pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot Block (000000-0001FFh) not protected from table reads executed in other blocks)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#define _XTAL_FREQ 8000000
#include <xc.h>
//#include <timers.h>
void init_timer0(void);
void main(void) {
OSCCONbits.IRCF = 0b111; //Set int clock to 8MHz)
ADCON1bits.PCFG = 0b1111111; //SET PORTA to digital i/o
//Set PORTA to output
TRISA=0;
PORTA=0;
//LATA=0;
LATAbits.LA0=1; //turn the diode on
init_timer0(); //init timer0 for 1s delay
while (1)
{
//Do nothing
//for a long time
};
return;
}
void init_timer0()
{
//Setting up the timer to 1s according to [url]http://www.enmcu.com/software/timer0calculatorandcodegeneration[/url]
T0CONbits.T08BIT = 0; //16bit
T0CONbits.T0CS = 0; //internal clock
T0CONbits.PSA = 0; //Prescaler on
T0CONbits.T0PS2 = 1; //1:32
T0CONbits.T0PS1 = 0;
T0CONbits.T0PS0 =0 ;
TMR0H = 0x0B; //set timer initial value to 3036 loading Low and High register with hex values
TMR0L = 0xDC;
//enabling the INterupt
IPEN=0;
INTCONbits.PEIE=1; //Peripheral int on
INTCONbits.TMR0IF=0; //timer0 flag clear
T0CONbits.TMR0ON = 1; //turn on the timer
INTCONbits.TMR0IE=1; //enable Timer0 Interupts
INTCONbits.GIE=1; //enable global interupts
}
void __interrupt() timer0(void)
{
//Toggle diode on RA0
if (INTCONbits.TMR0IF && INTCONbits.TMR0IE) //check if it timer0 overflow not needed but ...
{
TMR0H = 0x0B; //reload timer0 value to 3036 loading Low and High register with hex values
TMR0L = 0xDC;
if (LATAbits.LA0==1)
LATAbits.LA0=0;
else LATAbits.LA0=1; //Toggle the diode
INTCONbits.TMR0IF=0; //clear Timer0 interupt Flag
} ;
}
Debugger is working fine. But breadboard is whole different story.
The led is on always. It's not as bright as when connected straight to 5v and multimeter shows 1.3 volts. But It should go on and off every 1 second. So can any one check where I made mistake? I can't find it.