I'm currently having trouble with a piece of code trying to receive DMX(RS485). I have an LED blinking while the interrupt receiving the DMX from the transceiver (a MAX485) records data into an array. However, right now nothing in the main loop works when the interrupts are enabled.
- Code: Select all
/*
* File: main.c
* Author: Rusettsten
*
* Created on January 2, 2019, 9:48 PM
*/
// PIC16F15325 Configuration Bit Settings
#pragma config FEXTOSC = OFF // External Oscillator mode selection bits->Oscillator not enabled
#pragma config RSTOSC = HFINT1 // Power-up default value for COSC bits->HFINTOSC (1MHz)
#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 = OFF // Master Clear Enable bit (MCLR pin function is port defined 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 = ON // Brown-out reset enable bits (Brown-out Reset Enabled, SBOREN bit is ignored)
#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 = ON // Peripheral Pin Select one-way control (The PPSLOCK bit can be cleared and set only once in 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 BBSIZE = BB512 // ->512 words boot block size
#pragma config BBEN = OFF // ->Boot Block disabled
#pragma config SAFEN = OFF // ->SAF disabled
#pragma config WRTAPP = OFF // ->Application Block not write protected
#pragma config WRTB = OFF // ->Boot Block not write protected
#pragma config WRTC = OFF // ->Configuration Register not write protected
#pragma config WRTSAF = OFF // ->SAF not write protected
#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->UserNVM code protection disabled
#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
#include <pic.h>
#define _XTAL_FREQ 32000000
int maxAddress = 509;
int sleepTime = 2;
int timer2 = 77;
int dmxAddr = 6;
int DMXin[4];
int throw;
int readySwitch = 0;
/*
*
*/
unsigned int getDMX(){
while(!RC1IF);
throw = RC1STAbits.RX9D;
return RC1REG;
}
int main(void) {
//OSC Settings
// NOSC HFINTOSC; NDIV 1;
OSCCON1 = 0x60;
// CSWHOLD may proceed; SOSCPWR Low power;
OSCCON3 = 0x00;
// MFOEN disabled; LFOEN disabled; ADOEN disabled; SOSCEN disabled; EXTOEN disabled; HFOEN disabled;
OSCEN = 0x00;
// HFFRQ 32_MHz;
OSCFRQ = 0x06;
// MFOR not ready;
OSCSTAT = 0x00;
// HFTUN 0;
OSCTUNE = 0x00;
//Pin Settings
TRISA4 = 0;
ADCON0bits.ADON = 0;
ANSELCbits.ANSC5 = 0;
TRISC5 = 1;
BAUD1CONbits.BRG16 = 1;
TX1STAbits.BRGH = 1;
SP1BRGH = 1; // Set For High-Speed Baud Rate
SP1BRGL = 31; // Set The Baud Rate To Be 9600 bps
RC1STAbits.CREN = 1;//Setup for USART
TX1STAbits.SYNC = 0;
RC1STAbits.SPEN = 1;
RC1STAbits.RX9 = 1;
// Enable The Ascynchronous Serial Port
RC1IE = 1; // UART Receving Interrupt Enable Bit
PEIE = 1; // Peripherals Interrupt Enable Bit
GIE = 1; // Global Interrupt Enable Bit
//------------------
RA4 = 0;
while(1){
__delay_ms(100);
RA4 = 1;
__delay_ms(100);
RA4 = 0;
}
}
void __interrupt() ISR (void)
{
if(RC1IF == 1){
//throw = RC1STAbits.FERR;
// RC1STAbits.FERR = 0;
if(RC1STAbits.FERR == 1){
throw = getDMX();
// Destination = RCREG; // Read The Received Data Buffer
for(int x = 0; x <= 512; x++){
int dmxBuf = getDMX();
if(x == dmxAddr){
DMXin[0] = dmxBuf;//Gets DMX data into Array
}
if(x == dmxAddr + 1){
DMXin[1] = dmxBuf;//Gets DMX data into Array
}
if(x == dmxAddr + 2){
DMXin[2] = dmxBuf;//Gets DMX data into Array
}
if(x == dmxAddr + 3){
DMXin[3] = dmxBuf;//Gets DMX data into Array
}
}
}
RC1IF = 0; // Clear The Flag
}
}
Thanks in advance for any help you can provide. And yes I have looked in the manual for a solution.