Receiving DMX [EUSART] on a PIC16f15325

Enhanced mid-range devices. PIC12F1xxx and PIC16F1xxx

Receiving DMX [EUSART] on a PIC16f15325

Postby Rusettsten » Sat Jan 19, 2019 12:17 am

Hi everyone,

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.
Rusettsten
 
Posts: 4
Joined: Sat Jan 19, 2019 12:11 am

Re: Receiving DMX [EUSART] on a PIC16f15325

Postby ric » Sat Jan 19, 2019 9:29 am

Don't loop inside an interrupt service waiting for characters.
Receive ONE character per service, and get out again. A good ISR exits as soon as it can.
You will have to change your logic to keep track of what you are receiving each interrupt.

Also, this is pointless
Code: Select all
    RC1IF = 0;             // Clear The Flag

That is a read-only flag. The ONLY way to clear it is by reading RC1REG, which has already happened.
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: Receiving DMX [EUSART] on a PIC16f15325

Postby Rusettsten » Sat Jan 19, 2019 9:26 pm

Ahh, thanks Ric. I'll try it out.
Rusettsten
 
Posts: 4
Joined: Sat Jan 19, 2019 12:11 am

Re: Receiving DMX [EUSART] on a PIC16f15325

Postby Rusettsten » Sat Jan 19, 2019 9:48 pm

Hey! It works! Thanks. Finally got DMX to be received!
Rusettsten
 
Posts: 4
Joined: Sat Jan 19, 2019 12:11 am


Return to 14-Bit Core (enhanced)

Who is online

Users browsing this forum: No registered users and 8 guests

cron