I'm trying to get the Timer1 on my PIC18F2550 to fire interrupts every second for countdowns. So far I've been able to establish interrupt priority correctly, as well as other settings, however my ISR only fires once. I've set up my program to print the amount of seconds left in the countdown onto the LCD, so it's obvious to the user how many interrupts have passed. I've made sure to clear the proper interrupt flags in the ISR, since that's what was the source of the problem for the majority of the people having the same issue on google, but this didn't fix it. Everything else about my program has worked fine so far.
(For the sake of testing whether or not the right priority has fired, low counts up instead of down)
Here's the code snippet
//First testing program for PIC18F2550
#include <p18f2550.h>
#include <stdlib.h>
#include <delays.h>
#define _XTAL_FREQ 4915200
#pragma config PLLDIV = 1
#pragma config CPUDIV = OSC1_PLL2
#pragma config FOSC = XT_XT
#pragma config MCLRE = ON
#pragma config BOR = OFF
#pragma config WDT = OFF
#pragma config IESO = OFF
#pragma config PBADEN = OFF
#pragma config LVP = OFF
unsigned char Countdown = 30; (other vars, fcns omitted)
#pragma code high_vector = 0x08
void highvectinterrupt(void){
_asm goto highisr _endasm
}
#pragma code low_vector = 0x18
void lowvectinterrupt(void){
_asm goto lowisr _endasm
}
(...)
void highisr(void){
Countdown--;
PIR1bits.TMR1IF = 0;
}
void lowisr(void){
Countdown++;
PIR1bits.TMR1IF = 0;
}
void main(void){
UCONbits.USBEN=0; //Setup I/O
UCFGbits.UTRDIS=1;
OSCCONbits.SCS1 = 0;
OSCCONbits.SCS0 = 0;
BAUDCONbits.TXCKP=0;
SPBRG = 0x3F; //1200 baud
TRISA = 0x00;
TRISB = 0xFF;
TRISC = 0x00;
beephigh(); //Done, clear the line and beep speaker 1
LATA = 0; //reset
LATC = 0;
Delay10KTCYx(8);
LATAbits.LATA2 = 1; //load the reset
Delay10KTCYx(8);
LATAbits.LATA3 = 1; //stop reset
LATAbits.LATA4 = 1;
LATAbits.LATA2 = 0;
Delay10KTCYx(123);
initlcd();
Delay10KTCYx(10);
setupUI();
LATAbits.LATA0 = 0; //Done, clear the line and beep speaker 2
beeplow();
INTCON = 0; //Reset high interrupt flags and disable interrupts
T1CON = 0x11110100; //16 bit r/w; internal system clock; 8x prescale; timer1 osc disabled
TMR1H = 0; //Clear the counting register
TMR1L = 0;
PIE1bits.TMR1IE = 1; //Enable Interrupts on Timer1
PIR1bits.TMR1IF = 0; //Clear the flag if set
RCONbits.IPEN = 1; //Enable priority levels
INTCON2bits.TMR0IP = 0; //Timer0 on low priority
IPR1bits.TMR1IP = 0; //Timer1 on low priority
IPR1bits.RCIP = 1; //RS232 on high priority
INTCONbits.GIEL = 1; //Enable all low priority interrupts
INTCONbits.GIEH = 1; //Enable all high priority (must be enabled for low priority)
T1CONbits.TMR1ON = 1; //Turn Timer1 on
while(1){ //Idle and refresh screen
Delay10KTCYx(8);
bin2ascii(Countdown);
cmdlcd(0xCE);
putclcd(ConvRegTens);
putclcd(ConvRegOnes);
}
}
Ultimately I'm staring at an LCD with "31" on it. It's likely a single flag that I'm not setting properly that's causing nothing to work right, but as far as I've checked everything's being enabled and being cleared properly. What am I doing wrong? I'd appreciate any help, this is enormously frustrating.
Also: I've cleared all breakpoints so it's not halting anywhere, and program the latest version on compile. So I've ruled out running an old busted version of the code.
Edit: To clarify, this code is only for functionality; it will not fire every second but rather much faster. Once I get it working I'll play the overflow counting to get it to 1 second.
You are not finishing ISR routine correctly!
#pragma code low_vector = 0x18
void lowvectinterrupt(void){
_asm goto lowisr _endasm
}
After ISR execution the interrupt must be switched on again, so the lowvectinterrupt must be finished with RETFIE instruction and at least flags and WREG register must be restored.
Normaly is in ISR routine declared like:
static void interrupt isr(void)
{
}
Related
i'm currently trying to use a button as an interrupt. But i'm struggling with it. I've seen in the datasheet that i should enable: INTE bit of PIE0, INTEDG of INTCON, AND GIE if PIE.
I've also seen that i have to use the INTPPS register to configure a pin to be the interrupt, so as i want to use RB0 as an interupt, then i used PIR0bits.INTF to check if the interupt= true in my ISR. I did that :
void ledLoop(unsigned char *ptr){
unsigned char *tmp = ptr;
while(*tmp<64){
LATB = *tmp;
*tmp *= 2;
__delay_ms(200);
}
}
void __interrupt() high_isr(void){
INTCONbits.GIE = 0;
if( PIR0bits.INTF){
LATB = ~LATB;
PIR0bits.INTF = 0;
}
INTCONbits.GIE = 1;
}
void launch(void){
unsigned char run = 1;
while(1){
if(PORTDbits.RD7==1){
unsigned char tmp = 1;
ledLoop(&tmp);
}
if(PORTDbits.RD6==1){
LATB=0xff;
}
else{
LATB=0;
}
}
}
void main(void)
{
ANSELDbits.ANSD7=0;
ANSELDbits.ANSD6=0;
TRISB=0;
TRISA=0;
LATA=0x00;
TRISDbits.TRISD7=1;
TRISDbits.TRISD6=1;
TRISBbits.TRISB0=1;
//GIE: Global Interrupt Enable bit
//PEIE: Peripheral Interrupt Enable bit
PIE0bits.INTE = 1;
INTCONbits.GIE = 1;
INTCONbits.INTEDG = 1;
INTPPSbits.INTPPS = 0x08;
launch();
}
Any idea of what is wrong ?
using :
MPLABX IDE
XC8 Compiler
PIC16F18875
There are three thing wrong with the code you posted:
No configuration words, so code will not build as posted
Changing the GIE bit inside of an ISR
PORTB pin RB0 not configured for digital input
See my comments using /* */ for your code fixed:
/*
* File: main.c
* Author: dan1138
*
* Created on September 24, 2020, 2:23 PM
*/
// PIC16F18875 Configuration Bit Settings
// 'C' source line config statements
// CONFIG1
#pragma config FEXTOSC = OFF // External Oscillator mode selection bits (Oscillator not enabled)
#pragma config RSTOSC = HFINT32 // Power-up default value for COSC bits (HFINTOSC with OSCFRQ= 32 MHz and CDIV = 1:1) (FOSC = 32 MHz))
#pragma config CLKOUTEN = OFF // Clock Out Enable bit (CLKOUT function is disabled; i/o or oscillator function on OSC2)
#pragma config CSWEN = ON // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed)
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable bit (FSCM timer enabled)
// CONFIG2
#pragma config MCLRE = ON // Master Clear Enable bit (MCLR pin is Master Clear function)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config LPBOREN = OFF // Low-Power BOR enable bit (ULPBOR disabled)
#pragma config BOREN = OFF // Brown-out reset enable bits (Brown-out reset disabled)
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (VBOR) set to 1.9V on LF, and 2.45V on F Devices)
#pragma config ZCD = OFF // Zero-cross detect disable (Zero-cross detect circuit is disabled at POR.)
#pragma config PPS1WAY = OFF // Peripheral Pin Select one-way control (The PPSLOCK bit can be set and cleared repeatedly by software)
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will cause a reset)
// CONFIG3
#pragma config WDTCPS = WDTCPS_31// WDT Period Select bits (Divider ratio 1:65536; software control of WDTPS)
#pragma config WDTE = OFF // WDT operating mode (WDT Disabled, SWDTEN is ignored)
#pragma config WDTCWS = WDTCWS_7// WDT Window Select bits (window always open (100%); software control; keyed access not required)
#pragma config WDTCCS = SC // WDT input clock selector (Software Control)
// CONFIG4
#pragma config WRT = OFF // UserNVM self-write protection bits (Write protection off)
#pragma config SCANE = available// Scanner Enable bit (Scanner module is available for use)
#pragma config LVP = ON // Low Voltage Programming Enable bit (Low Voltage programming enabled. MCLR/Vpp pin function is MCLR.)
// CONFIG5
#pragma config CP = OFF // UserNVM Program memory code protection bit (Program Memory code protection disabled)
#pragma config CPD = OFF // DataNVM code protection bit (Data EEPROM code protection disabled)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#define _XTAL_FREQ (32000000ul)
void ledLoop(unsigned char *ptr){
unsigned char *tmp = ptr;
while(*tmp<64){
LATB = *tmp;
*tmp *= 2;
__delay_ms(200);
}
}
void __interrupt() high_isr(void){
//INTCONbits.GIE = 0; /* <= do not do this in an interrupt service routine */
if((PIE0bits.INTE) && ( PIR0bits.INTF)){ /* check that the interrupt is enabled AND asserted */
LATB = ~LATB;
PIR0bits.INTF = 0;
}
//INTCONbits.GIE = 1; /* <= do not do this in an interrupt service routine */
}
void launch(void){
unsigned char run = 1;
while(1){
if(PORTDbits.RD7==1){
unsigned char tmp = 1;
ledLoop(&tmp);
}
if(PORTDbits.RD6==1){
LATB=0xff;
}
else{
LATB=0;
}
}
}
void main(void)
{
ANSELDbits.ANSD7=0;
ANSELDbits.ANSD6=0;
TRISB=0;
TRISA=0;
LATA=0x00;
TRISDbits.TRISD7=1;
TRISDbits.TRISD6=1;
TRISBbits.TRISB0=1;
ANSELBbits.ANSB0=0; /* make RB0 a digital input */
//GIE: Global Interrupt Enable bit
//PEIE: Peripheral Interrupt Enable bit
PIE0bits.INTE = 1;
INTCONbits.GIE = 1;
INTCONbits.INTEDG = 1;
INTPPSbits.INTPPS = 0x08; /* assign INT input to RB0 */
launch();
}
I am trying to implement error correction over an r/f communication between two arduinos. I tried adding a timer to it, in order to create a packet resend, but whenever it gets past the first send, it starts printing garbage ad infinity instead of doing the timer interrupt.
I tried messing around with the inside loop conditions some as well as trying to figure out what was wrong with the timer, but I couldn't figure it out. The problem seems to happen right around the first serial print, which is strange, because that part of the code is mostly unchanged.
(packets is a structure of two ints)
#include <ELECHOUSE_CC1101.h>
#include "packets.h"
// These examples are from the Electronics Cookbook by Simon Monk
// Connections (for an Arduino Uno)
// Arduino CC1101
// GND GND
// 3.3V VCC
// 10 CSN/SS **** Must be level shifted to 3.3V
// 11 SI/MOSI **** Must be level shifted to 3.3V
// 12 SO/MISO
// 13 SCK **** Must be level shifted to 3.3V
// 2 GD0
const int n = 61;
unsigned short int sequence = 0;
byte buffer[n] = "";
void setup() {
Serial.begin(9600);
Serial.println("Set line ending to New Line in Serial Monitor.");
Serial.println("Enter Message");
ELECHOUSE_cc1101.Init(F_433); // set frequency - F_433, F_868, F_965 MHz
// initialize timer1
noInterrupts(); // disable all interrupts
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
OCR1A = 0xFFFF; // Max value for overflow for now
TCCR1B |= (1 << CS12); // 256 prescaler
interrupts(); // enable all interrupts
}
Packet pckt, recieve;
ISR(TIMER1_OVR_vect){ // timer compare interrupt service routine
//Resend packet
ELECHOUSE_cc1101.SendData(buffer, pckt.data + pckt.seqNum);
int len = ELECHOUSE_cc1101.ReceiveData(buffer);
buffer[len] = '\0';
recieve.seqNum = buffer[n];
Serial.println("Interrupt");
}
void loop() {
if (Serial.available()) {
pckt.data = Serial.readBytesUntil('\n', buffer, n);
pckt.seqNum = sequence;
buffer[pckt.data] = '\0';
buffer[n-1] = pckt.seqNum;
Serial.println((char *)buffer);
ELECHOUSE_cc1101.SendData(buffer, pckt.data + pckt.seqNum);
TCNT1 = 0; // clear timer
TIMSK1 |= (1 << TOIE0); // enable timer compare interrupt
int len = ELECHOUSE_cc1101.ReceiveData(buffer);
while (recieve.seqNum <= sequence) {
}
TIMSK1 &= ~(1 << TOIE0); // turn off the timer interrupt
}
}
Sending data takes too long for interrupts. You should keep calls to send and receive buffers of data within the loop() function call tree. For example, sending a 12 bytes message via UART at 9600 bauds can take up to about 12ms.
You can use the timer interrupt to decrement a timeout counter, as is usually done on micro controllers, or use the millis() function to handle timings, as is easily done on Arduino.
I suggest you use the millis() function to compute timeouts.
example:
/* ... */
// I could not figure out what you were trying to do with
// pckt.seqNum.... Putting it at the end of the buffer
// makes no sense, so I've left it out.
// Moreover, its size is 2, so placing it at buffer[n-1] overflows the buffer...
enum machineState {
waitingForSerial,
waitingForResponse,
};
unsigned int time_sent; // Always use unsigned for variables holding millis()
// can use unsigned char for timeouts of 255
// milliseconds or less. unsigned int is good for about
// 65.535 seconds or less.
machineState state = waitingForSerial;
void loop()
{
switch(state)
{
case waitingForSerial:
pckt.data = Serial.readBytesUntil('\n', buffer, sizeof(buffer));
if (pckt.data > 0)
{
++pckt.seqNum;
Serial.write(buffer, pckt.data);
ELECHOUSE_cc1101.SetReceive();
ELECHOUSE_cc1101.SendData(buffer, pckt.data);
time_sent = millis();
state = waitingForResponse;
}
break;
case waitingForResponse:
if (ELECHOUSE_cc1101.CheckReceiveFlag())
{
auto len = ELECHOUSE_cc1101.ReceiveData(buffer)) // can use C++17 with duinos!!!
Serial.print("cc1101: ");
Serial.write(buffer, len);
state = waitingForSerial; // wait for another command from PC
}
// 1 second timeout, note the cast and subtraction, this is to avoid any
// issues with rollover of the millis() timestamp.
else if ((unsigned int)millis() - time_sent > 1000)
{
// resend ... stays stuck this way.
Serial.println("Retrying :(");
ELECHOUSE_cc1101.SendData(buffer, pckt.data);
time_sent = millis();
}
break;
default:
state = waitingForSerial;
Serial.println("unhandled state");
break;
}
}
HI I'm trying to implement simple spi communication to turn on 8 led connected to shift register can amy one help me whats wrong with this code, im getting random output, I want to send 0x02 to turn on led(00000010) .
#include <xc.h>
#include "spi.h"
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = OFF // Low-Voltage In-Circuit Serial Programming Enable bit
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit
#pragma config WRT = OFF // Flash Program Memory Write Enable bits
#pragma config CP = OFF // Flash Program Memory Code Protection bit
#define _XTAL_FREQ 8000000
void main()
{
char data = 0x10;
spiInit(SPI_MASTER_OSC_DIV4, SPI_DATA_SAMPLE_END, SPI_CLOCK_IDLE_LOW, SPI_IDLE_2_ACTIVE);
TRISC0 = 0;
RC0 = 1;
RC3 = 0;
RC5 = 0;
while(1)
{
RC0 = 0; //Slave Select
__delay_ms(100);
spiWrite(data);
while(!SSPSTATbits.BF);
__delay_ms(100);
RC0= 1; //Slave Deselect
}
}
I am trying to transmit a character "a" from pic16f887 and see the result on the terminal, but all I get is a question mark(using USART terminal), or nothing at all(Putty, Hyperterminal). I am not so great with C, as I'm only a beginer, but I really need to get this working for my school project.. I have tried many codes I've found over the internet, and I did manage to receive a character from the terminal and, lets say, turn on a LED, but I just can't manage to make it send anything. And I have a strong feeling its somewhere in the code.. Im using MPLAB and Hi-Tech C compiler to build the project. Here it is:
unsigned char cUART_char;
unsigned char cUART_data_flg;
void init_uart(void);
void UART_putc(unsigned char c);
void InterruptHandlerLow ();
void main()
{
TRISA = 0;
PORTA = 0;
TRISB = 0;
PORTB = 0;
TRISD = 0;
PORTD = 0;
ANSELH = 0;
init_uart();
while (1)
{
if (cUART_data_flg==1)
{
UART_putc(cUART_char);
cUART_data_flg=0;
}
}
}
void InterruptHandlerLow ()
{
if (RCIF==1)//is interrupt occured by EUSART receive?,
//then RCREG is full we have new data (cleared when RCREG is read)
{
if(RCSTA&0x06) //more efficient way than following commented method to check for reception error
//if(RCSTAbits.FERR==1 || RCSTAbits.OERR==1 )
{
CREN=0; //Overrun error (can be cleared by clearing bit CREN)
cUART_char=RCREG; //clear Framing error
CREN=1;
}
else
{
cUART_char = RCREG; // read new data into variable
cUART_data_flg = 1; // new data received. so enable flg
}
}
}
void init_uart(void) // init UART module for 9600bps boud, start bit 1, stopbit 1, parity NONE
{
// init data receive flag to zero (no data)
TRISC7=1; //Make UART RX pin input
TRISC6=0; //Make UART TX pin output
SYNC = 0; // enables for asynchronous EUART
SPEN = 1; // enables EUSART and sets TX (RC6) as output; ANSEL must be cleared if shared with analog I/O
CREN = 1;
TX9 = 0; // 8bit mode
RX9 = 0;
TXEN = 1; // enables Transmitter
BRGH = 1; // baud rate select
BRG16 = 0;
SPBRG = 25; //baud rate select 9600#4Mhz
SPBRGH = 0;
RCIE=1; // receive interrupt enable
GIE=1; // global interrupt enable
PEIE=1 ; // Peripheral Interrupt Enable bit
}
void UART_putc(unsigned char c)
{
TXEN=0;// disable transmission
TXREG=0x61; // load txreg with data
TXEN=1; // enable transmission
while(TRMT==0) // wait here till transmit complete
{
}
}
Please if someone sees a problem (or more) in this code, help me to crack this ;)
Oh and I am transmitting when pressing a button connected to a TX pin (RC6)..
I'm using the PIC18F4550 from microchip with the c compiler: by default the 'program memory' is used, but now I want to use the 'data memory' because it gives me a big more functionality, can enyone just tell me how can I to that in the program language C.
That the code:
#pragma code
/******************************************************************************/
void main (void)
{
TRISD = 0x00; // PORTD als uitgang
TRISB = 0b00110000; // RB4 en RB5 als ingang
TRISA = 0x00; // RA output
RCONbits.IPEN = 0; // prioriteit uit
INTCONbits.GIE = 1; // enable interrupt
INTCONbits.RBIE = 1; // interrupt portB aan
while(1)
{
_asm sleep _endasm
}
}
#pragma interrupt ISR
void ISR (void)
{
int rood[] = {0,0,1,0,1,0,1,0,1,0,1,1,1,0,0,0,0,1,1,0,1,0,1,0,1,0,0,0,1,1,0,1,1,1,0,1,0,1,0,1,0,0,0,1,1,1,1,0,1,1,};
if (INTCONbits.RBIF==1)
{
if(PORTBbits.RB5==0) // S3 ingedrukt ?
{
int i = 0;
int b;
do {
LATAbits.LATA2 = rood[i];
LATDbits.LATD1 ^= 1;
b = 0;
do {
b++;
}while(b <= 1500);
i++;
}while(rood[i] <= 50);
//LATDbits.LATD1 ^= 1; // D2 togglen
}
}
INTCONbits.RBIF = 0;
}
The data sheets for this device seem to indicate that the Data Memory is static ram, and that there is no data path from there to the instruciton decode logic, ie, the data memory cannot be used to store program code for execution. If that was your goal, you are out of luck.
In terms of allocating volatile storage, since the program memory does not appear to include any RAM, my guess is that the C compiler will automatically allocate all variables, etc from the data ram.
That leaves only the choice between the "data" EEPROM and the "program" flash for storage of non-volatile (or semi-volatile) data. There you may have to dig into the documentation - it could be this is done with compiler pragmas, or a linker map file, or it could be that access has to be handled indirectly and explicitly through registers.