HSERIN & Interupts (aka controlling PIC programs from a remote PC)


Closed Thread
Results 1 to 17 of 17

Hybrid View

  1. #1
    Join Date
    Mar 2009
    Posts
    653

    Default HSERIN & Interupts (aka controlling PIC programs from a remote PC)

    So I started out on this journey with the intention of kludging a little PIC program to simply count encoder pulses from a striped disc attached to a motor then traversing the copper wire feeding onto it viaa stepper motor ...alas, I knew nothing about stepper nor PICs! Well - thanks to a lot of help on here - my PIC program now works....as the main motor turns, the little stepper motor is 'synced' (via the encoder & pic 'counter'), meaning that copper wire is fed onto the coil's core in 'Good King Wenceslas' style (ie deep & crisp & even - geddit?!!). I'll post a video soon.

    but wait ...that's not good enough!

    Yep - it's bells & whistles time!! [crowd groans]

    At the minute, I have some simple push buttons hardwired into my circuit to stop & start the PIC program ..."it's good, but it's not quite right" (UK TV viewers might lock onto that phrase).

    I actually would like a few more button options...sure I could crank up the button count...but that's going to get , erhm buttony! Guess what...my PC has lots of buttons (though most people call them keys)

    in short I'd like to control my PIC from my PC over serial comms into the h/w USART using interupts....so that when for example I press an 'S' key on my PC, the PIC program stops, or a 'P' key...it pauses etc

    Now then, I used interupts with my little PIC program already (for every time there's a black stripe arrives from the encoder wheel, a little interupt is raised...much neater than polling)...but I learnt bo-diddley about interupts. I kludged & 'borrowed' from others' PIC programs...coupled with the help I got on my last thread ...i got there (ie "The worst programmer in the world" thread...which I still am!)

    So, I need to try & wrap my head around the mechanism behing DT's interupt routines (he's done a fantastic job, which places him above criticism...but I'd like to say that as a newbie...we could do with couple more examples coupled with more explanation aimed at an amoeba level!)

    Ok, so I went to my best friend...a bloke called Google (though Bing crosby might be my new best friend soon...I'm fickle that way). And this thread turns up...

    http://www.letbasic.com/forum/showthread.php?p=73546

    which had an uber simple example of just the type of code I need to get me started. So in the best tradition of plagiarism, I tried to use it - it didn't work for me.

    (Now the author did declare that he hadn't tested it...but I'm figuring if I can get this simple little interupt routine to work with my USART & HSERIN, then I'm laughing....)

    Can you help?

    my PIC is a 16f690 ...I reckon the switches/fuses are ok, as I've got HSERIN working with another (non interupt based) PIC program.

    when the 1 key is pressed, I should see something (either "got it" or "didn't get it" onscreen - I see neither)

    @MyConfig = _XT_OSC & _WDT_ON & _MCLRE_ON & _CP_OFF
    @MyConfig = MyConfig & _MCLRE_ON & _BOR_OFF
    @ __config MyConfig

    DEFINE OSC 4
    DEFINE HSER_SPBRG 25
    DEFINE HSER_TXSTA 24h
    DEFINE HSER_CLROERR 1
    ANSELH=0
    ANSEL=0
    CM1CON0 =0
    CM2CON0 =0
    CM2CON1 =0
    adcon1=0
    TRISB.6 = 1
    TRISB.7 = 0
    TRISC=%00000000 ; set all Port C pins as outputs
    rcsta.7=1 'SPEN serial port enable bit
    low portc.0
    low portc.1
    low portc.2
    low portc.3

    mybyte var byte

    '::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::::::::::
    ASM
    INT_LIST macro ; IntSource, Label, Type, ResetFlag?
    INT_Handler RX_INT, _Get_char, PBP, yes
    endm
    INT_CREATE ; Creates the interrupt processor
    ENDASM

    '::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::::::::::

    @ INT_ENABLE RX_INT ; enable UART RX interrupt


    loop:
    if mybyte="1" then
    hserout ["got it!"]
    mybyte=0
    endif
    if mybyte<>"1" then
    hserout ["didn't get it!"]
    mybyte=0
    endif
    goto loop


    '---[USART RX Interrupt handler]----------------------------------------------------
    Get_char:
    hserin 100,noreceived,[mybyte] 'Get byte
    noreceived: 'or if timeout return
    @ INT_RETURN



    Edit: If I strip all the interupt aspect out from above, and add in a simple

    start1:
    pause 999
    HSEROUT [13,10]
    HSEROUT ["Test, 13,10]
    goto start1


    in place of the original loop...I see the word "Test" on my screen...this proves the config/fuses to do with HSEROUT are correct at least!
    Last edited by HankMcSpank; - 16th June 2009 at 14:48.

  2. #2
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default It's all in DT-Ints.

    Code:
    '::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::::::::::
    ASM
    INT_LIST macro ; IntSource, Label, Type, ResetFlag?
    <font color=red>INT_Handler RX_INT, _Get_char,  PBP,  yes</font color>
    endm
    INT_CREATE ; Creates the interrupt processor
    ENDASM
    
    '::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::::::::::
    
    <font color=blue>@ INT_ENABLE RX_INT ; enable UART RX interrupt
    </font color>
    The code in red is the receive interrupt, open DT-INTS and READ the options available, in amongst those are RB and INT choices, add them the way Darrel wrote them, into your interrupt macro, then enable them as done in Blue.
    Example:
    Code:
    '::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::::::::::
    ASM
    INT_LIST macro ; IntSource, Label, Type, ResetFlag?
    INT_Handler RX_INT, _Get_char, PBP, yes
    INT_HANDLER INT_INT, _MyNewLabel, PBP,yes
    endm
    INT_CREATE ; Creates the interrupt processor
    ENDASM
    
    '::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::::::::::
    
    @ INT_ENABLE RX_INT ; enable UART RX interrupt
    @ INT_ENABLE INT_INT ;enable External Interrupts
    Here is your selection snipped from DT-Ints14
    Code:
      #define INT_INT   INTCON,INTF     ;-- INT External Interrupt
      #define RBC_INT   INTCON,RBIF     ;-- RB Port Change Interrupt
      #define TMR0_INT  INTCON,T0IF     ;-- TMR0 Overflow Interrupt 16F
      #define TMR1_INT  PIR1,TMR1IF     ;-- TMR1 Overflow Interrupt
      #define TMR2_INT  PIR1,TMR2IF     ;-- TMR2 to PR2 Match Interrupt
      #define TX_INT    PIR1,TXIF       ;-- USART Transmit Interrupt
      #define RX_INT    PIR1,RCIF       ;-- USART Receive Interrupt
      #define CMP_INT   PIR2,CMIF       ;-- Comparator Interrupt
      #define EE_INT    PIR2,EEIF       ;-- EEPROM/FLASH Write Operation Interrupt
      #define BUS_INT   PIR2,BCLIF      ;-- Bus Collision Interrupt
      #define PSP_INT   PIR1,PSPIF      ;-- Parallel Slave Port Read/Write Interrupt
      #define AD_INT    PIR1,ADIF       ;-- A/D Converter Interrupt
      #define SSP_INT   PIR1,SSPIF      ;-- Master Synchronous Serial Port Interrupt
      #define CCP1_INT  PIR1,CCP1IF     ;-- CCP1 Interrupt
      #define CCP2_INT  PIR2,CCP2IF     ;-- CCP2 Interrupt
    INT_Handler RX_INT, _Get_char, PBP, yes tells the PIC<b> WHICH</b> interrupt to use and <b>WHICH</b> label to jump to.
    Last edited by Archangel; - 16th June 2009 at 18:03.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  3. #3
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    Many thanks Joe...that actually helped a lot.

    That program I posted above doesn't function this end at all - & I can't for the life of me work out why.

    Not sure if it's related to some compile errors I was getting. I managed to get rid of a couple of compile errors by removing the space between the @ & the command, ie....

    @ INT_ENABLE RX_INT becomes @INT_ENABLE RX_INT

    but I'm still seeing this one during the compile...


    Error[122] c:\progra~1\mecani~1\mcs\inter~2.asm 109 :Illegal opcode [RX_INT]

    Any ideas?

  4. #4
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,807


    Did you find this post helpful? Yes | No

    Default

    This:

    Code:
    @MyConfig = _XT_OSC & _WDT_ON & _MCLRE_ON & _CP_OFF 
    @MyConfig = MyConfig & _MCLRE_ON & _BOR_OFF 
    @ __config  MyConfig
    should be:


    Code:
    @MyConfig1 = _XT_OSC & _WDT_ON & _MCLRE_ON & _CP_OFF 
    @MyConfig2 = MyConfig & _MCLRE_ON & _BOR_OFF 
    @ __config  MyConfig1 & MyConfig2
    Also where are your include files for the DT-INTS ?

    Be sure to select the correct chip before compilation.


    Ioannis

  5. #5
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    Thanks for taking the time to respond.

    Ok, I made that change to the header config. (I already had the correct PIC variant selected)

    The include DT bits where in my original program (I cut/pasted a last minute 'just before bed' effort into my post above, where I'd obviously not put that bit in). Actually, now that I have put them back in, I'm getting even more compilation errors - see this screen scrape...

    (it's a little blurry - but the image can be enlarged a bit more by clicking on the image after opening it the first time)

    here's the code (by the way, which option is everyone choosing when they paste iup their code to have it in a box?!)...

    @MyConfig1 = _XT_OSC & _WDT_ON & _MCLRE_ON & _CP_OFF
    @MyConfig2 = MyConfig & _MCLRE_ON & _BOR_OFF
    @ __config MyConfig1 & MyConfig2

    INCLUDE "DT_INTS-14.bas" ; Base Interrupt System
    INCLUDE "ReEnterPBP.bas" ; Include if using PBP interrupts

    DEFINE OSC 4
    DEFINE HSER_SPBRG 25
    DEFINE HSER_TXSTA 24h
    DEFINE HSER_CLROERR 1
    ANSELH=0
    ANSEL=0
    CM1CON0 =0
    CM2CON0 =0
    CM2CON1 =0
    adcon1=0
    TRISB.6 = 1
    TRISB.7 = 0
    TRISC=%00000000 ; set all Port C pins as outputs
    rcsta.7=1 'SPEN serial port enable bit
    low portc.0
    low portc.1
    low portc.2
    low portc.3

    mybyte var byte

    '::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::::::::::
    ASM
    INT_LIST macro ; IntSource, Label, Type, ResetFlag?
    INT_Handler RX_INT, _Get_char, PBP, yes
    endm
    INT_CREATE ; Creates the interrupt processor
    ENDASM

    '::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::::::::::

    @INT_ENABLE RX_INT

    loop:
    if mybyte="1" then
    hserout ["got it!"]
    mybyte=0
    endif
    if mybyte<>"1" then
    hserout ["didn't get it!"]
    mybyte=0
    endif
    goto loop


    '---[USART RX Interrupt handler]----------------------------------------------------
    Get_char:
    hserin 100,noreceived,[mybyte] 'Get byte
    noreceived: 'or if timeout return
    @INT_RETURN
    Last edited by HankMcSpank; - 17th June 2009 at 10:45.

  6. #6
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,807


    Did you find this post helpful? Yes | No

    Default

    sorry I type what you already had as a mistake!

    corrected:

    @MyConfig1 = _XT_OSC & _WDT_ON & _MCLRE_ON & _CP_OFF
    @MyConfig2 = _MCLRE_ON & _BOR_OFF
    @ __config MyConfig1 & MyConfig2

    or better

    @ __config = _XT_OSC & _WDT_ON & _MCLRE_ON & _CP_OFF & _MCLRE_ON & _BOR_OFF

    For the boxed code use square brackets [ ] and the tags code and /code for the start and end part.

    You do have MPASM selected, right/

    Ioannis

  7. #7
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    Still no good - errors in spades!



  8. #8
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Got a couple problems going on here ...

    First the funky Config lines...
    I came up with that quite some time ago as a way to shorten the __CONFIG lines in the editor.
    It's been passed around a bit, and Hank had it right(almost) in the first post.
    With the exception that _MCLRE_ON was duplicated(not a problem) it was perfect.

    After time moves on, I tend to do it more like this now ... (16F690)
    Code:
    ASM
    cfg=      _XT_OSC     ; Oscillator Selection
    cfg=cfg&  _FCMEN_OFF  ; Fail-Safe Clock Monitor
    cfg=cfg&  _IESO_OFF   ; Internal External Switchover
    cfg=cfg&  _BOR_OFF    ; Brown-out Reset
    cfg=cfg&  _CPD_OFF    ; Data Code Protection
    cfg=cfg&  _CP_OFF     ; Code Protection
    cfg=cfg&  _MCLRE_ON   ; Master Clear Reset
    cfg=cfg&  _PWRTE_ON   ; Power-up Timer
    cfg=cfg&  _WDT_OFF    ; Watchdog Timer
      __CONFIG  cfg
    ENDASM
    The second problem here is Indentation.

    The forum tends to scrunch everything up to the left side if you don't use a [code][/code] block.
    Copying snippets from the forum often leads to problems if you forget about the indentation. Especially when it's ASM code.

    Take a look at the original examples for DT_INTS and you can see the proper indentation for the ASM statements.

    Indentation applies to the @ operator too.
    Removing the space between the @ and INT_ENABLE etc, does not do what you want it to do. That space must be there.
    <br>
    DT

Similar Threads

  1. Replies: 26
    Last Post: - 2nd October 2017, 11:35
  2. Direct PIC to PC without MAX232
    By acjacques in forum Serial
    Replies: 14
    Last Post: - 23rd October 2014, 21:32
  3. Send data PIC to PC
    By konter in forum Off Topic
    Replies: 6
    Last Post: - 25th December 2009, 22:04
  4. Replies: 67
    Last Post: - 8th December 2009, 02:27
  5. Controlling power to a PIC with another PIC
    By jswayze in forum Off Topic
    Replies: 3
    Last Post: - 28th May 2005, 19:44

Members who have read this thread : 1

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