Serial UART and Interupts on a 16F87X


Results 1 to 14 of 14

Threaded View

  1. #9
    Join Date
    Jul 2003
    Posts
    2,405

    Default Re: Serial UART and Interupts on a 16F87X

    Here's a untested/simplfied version that should work. Compare it to your original to see the changes.
    Code:
    DEFINE OSC 4 ' 4 MHz clock
    DEFINE ADC_BITS 10 ' Set number of bits in result
    DEFINE ADC_CLOCK 3 ' Set clock source (3=rc)
    DEFINE ADC_SAMPLEUS 20 ' Set sampling time in uS
    DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
    DEFINE HSER_TXSTA 20h ' Enable transmit, BRGH = 0
    DEFINE HSER_SPBRG 25 ' 2400 Baud @ 4MHz, 0.17%
    DEFINE HSER_CLROERR 1 ' Clear overflow automatically
     
    TRISA = %000001     ' Set PORTA
    TRISB = %01100000   ' Set PORTB
    TRISC = %10000000   ' Set PORTC
    ADCON1 = %10001110  ' RA0 A/D input. +Vref=Vdd, -Vref=Vss. Right justified for 10-bit
    INTCON = %11000000  ' Global & peripheral ints enabled. Not RB0/INT
    PIE1 = %00100000
     
    '**************** DEFINE VARIABLES ***************************************
    relay VAR BYTE 'relay number storage variable
    stat VAR BYTE 'relay status ON/OFF variable
    POTADC VAR WORD 'ADC Result
    RXBYTE var byte
     
    '**************** PORT B Setup *******************************************
    ANT4 VAR PORTB.3 'ANT4 Relay
    ANT3 VAR PORTB.2 'ANT3 Relay
    ANT2 VAR PORTB.1 'ANT2 Relay
    ANT1 VAR PORTB.0 'ANT1 Relay 
     
    '**************** PORT C Setup *******************************************
    CCW VAR PORTC.5 'CCW Relay
    CW VAR PORTC.4 'CW Relay
     
    '****************** PORT SETUP *******************************************
    CW=0 ' CW Relay Off
    CCW=0 ' CCw Relay Off
    ANT1=0
    ANT2=0
    ANT3=0
    ANT4=0
    ADCON0=0 ' Set up ADCON1
    ON INTERRUPT GOTO RECEIVE
     
    MAIN:
      ADCIN 0, POTADC 'convert ADC value to a byte value: 
      HSEROUT [DEC POTADC,32] 'TX DATA on PORTC.6
      PAUSE 300 
      GOTO MAIN
     
    DISABLE  ' <-- doesn't matter if DISABLE is before or after the label
             ' just as long as it's before any CODE.
    RECEIVE: 
      IF RCSTA.2=1 THEN
         HSERIN [RXBYTE]
      ELSE
         HSERIN 500,IntExit,[relay,stat] 'Serial data in on PortC.7
      ENDIF
     
      ' Simplified version without gosubs, gotos, etc.
      IF relay = 1 THEN CW = Stat.0       ' Process relay#1
      IF relay = 2 THEN CCW = Stat.0      ' Process relay#2
      IF relay = 3 THEN PORTB = %00000001 ' Process relay#3
      IF relay = 4 THEN PORTB = %00000010 ' Process relay#4 
      IF relay = 5 THEN PORTB = %00000100 ' Process relay#5
      IF relay = 6 THEN PORTB = %00001000 ' Process relay#6
     
    IntExit:
      RESUME
      ENABLE ' <-- You only need ENABLE if you place more code below
             ' it and you want interrupt checking code there.
      END
    Last edited by Bruce; - 31st August 2011 at 12:23.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts