ICD bits -- ADC and 16F886 E/SP.

Discussion in 'Embedded Systems and Microcontrollers' started by R!f@@, Jan 3, 2016.

  1. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    8,750
    759
    I ran into a road block.
    How do you set the bits in ICSPDAT and ICSPCLK.
    I have the ICD enabled in config.
    What will happen if I set them as Inputs in TRISB

    And about the comparator.....
    Code (Text):
    1.   ANSEL = 0x1F;                      // AN0 to AN4 as Analog Inputs.
    2.   CM1CON0 = 0x07;                    // Comparator 1 off
    3.   CM2CON0 = 0x07;                    // Comparator 2 off
    4.   TRISA = 0xFF;                      // Port A as Inputs
    5.   TRISB = 0xDC;                      // TrisB 'b11011100'
    6.   TRISC = 0x00;                      // Port C as Outputs
    I plan to use the ADC.
    Am I on the right track here.
    uC is 16F886 E/SP
     
    Last edited: Jan 3, 2016
  2. JohnInTX

    Moderator

    Jun 26, 2012
    2,341
    1,024
    If you are using the ICSP pins for programming only, all you have to worry about is that the shared circuits are compatible (see the ICSP stuff at Microchip). Once the chip is programmed in then non-debug mode, the pins are just like any others.

    If you are using the ICSP pins in the debug mode (CONFIG : DEBUG/ ==0), you can't use the pins for anything else while you are debugging- just set the TRIS to 1 and be done with it. Is not recommended that you have any other circuitry on the pins (you can but you have to be careful). Of course, this means that when you release the code (with the debugger turned off) you have a couple of floating inputs and that's not good. My solution is to have a compile-time switch for DEBUG/RELEASE that re-configures the two pins as output-0 when RELEASE is selected.

    If you need the pins in your release version it gets a bit more involved. In those cases, I put some non important thing on the lines (status LED, mode jumpers etc) with ICSP compatible isolation. For example if you had a jumper that selected an operating mode, you could locate it on an ICSP pin. For DEBUG, pull the jumper and use another compile time switch to manually select the jumper value for your debug session. For RELEASE, turn on the weak pullup and read the pin for the jumper value.

    Example:
    Code (C):
    1.  
    2. #define ManualJumperValue 1 // change and rebuild to simulate jumper in/out
    3.  
    4. bit JumperIn(void)
    5. {
    6. #ifdef DEBUG
    7.       return(ManualJumperValue);
    8.   #else
    9.       return(PORTB & 0x80);
    10.   #endif
    11. }
    That kind of thing. I collect all of the IO routines in their own file and all of the configuration stuff in a master config file that can be edited as required. I also have a second config file for RELEASE versions. When building for release, it replaces all of the debug stuff that I've been messing with with all of the values for a normal release version. That way, I can be stupid and forgetful without worrying about resetting all of the debug stuff back to a proper RELEASE version.

    Note that MPLAB will provide a command line defined value (__DEBUG or something like that..) according to the project/toolbar settings. MikroC may do that as well but personally, I use my own definition in the config file. Just as easy and the syntax never changes.

    Finally, you can confirm your builds by using the #warning directive to send a message to the build output window:
    Code (C):
    1.  
    2. #ifdef DEBUG
    3.     #warning ("Project built for DEBUGGING.")
    4.   #endif
    5.  
    My code has lots of this kind of stuff and is quite a chatterbox when building for non-release targets.


    Hope that helps.
     
    Last edited: Jan 3, 2016
  3. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    8,750
    759
    ooops ! Never realized I was in the wrong forum. :oops: My bad. Had a lotta work when I decided to ask ahead. Thanks Scott.

    Egad ! I ask a simple thing and you made my head spin :eek:.
    May be I am hungry. :p Long day.
    I will go thru the post at home ( with in an hour ) and when my stomach is full, I might get this thru my head.

    PS. The Debug pin is for debug only. Nothing is connected to it and never will be. I showed the schema in the other thread. About using the ICSP.
    You can say the setting here


    By the way how are my comparator values ?
     
  4. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    8,750
    759
    So I started to write.
    First just to see if the buzzer and LCD is working.
    As usual it did not. It was Programing and verifying OK.
    So Put the PIC back into EasyPIC. Did not help. It was going haywire.

    So I rewrote like this to test the PIC

    Code (Text):
    1.  
    2.  
    3. void initIO(){
    4.   ANSEL = 0x00;
    5.   CM1CON0.C1ON = 0;  // Disable comparator 1
    6.   CM2CON0.C2ON = 0;  // Disable comparator 2
    7.   PORTA = 0;  // Clear Ports
    8.   PORTB = 0;  // Clear Ports
    9.   PORTC = 0;  // Clear Ports
    10.   TRISA = 0x00;
    11.   TRISB = 0x00;
    12.   TRISC = 0x00;
    13. }
    14. void main(){
    15.   initIO();  // Initiate I/O's
    16.  
    17.   while(1){
    18.   PortA = ~PortA;
    19.   PortB = ~PortB;
    20.   PortC = ~PortC;
    21.   delay_ms(1000);
    22.   PortA = ~PortA;
    23.   PortB = ~PortB;
    24.   PortC = ~PortC;
    25.   delay_ms(1000);
    26.   }
    27. }
    28.  
    Funny part is PORTA and PORTC are flashing
    PORTB 's RB7 and 6 are flashing while RB 5 to 0 are lit o_O

    I tried 3 pc of PIC. Still the same.
    I do not understand what I am doing wrong :confused:
    One more thing. I had disable the debug to get it working
     
    Last edited: Jan 3, 2016
  5. JohnInTX

    Moderator

    Jun 26, 2012
    2,341
    1,024
    re: #4
    You need to set ANSELH to make PORTB digital.
    If debug is enabled you have to be connected to a debugger and build for debugging. When DEBUG/ is 0 in config, it expects a debugger to be present... I think in MikroC you have to change the config bits AND the programmer settings to switch from debug to stand-alone mode. I don't have the MicroICD so don't know.
    Other than that it should be OK.

    re: #3
    For the 887, set CxON in CMxCON0 to 0 to disable the comparators. Other PICs use the 0x7 figure.
    If you are dedicating the ICSP pins to ICSP, leave them as inputs for now. We'll have to look up to see if DEBUG/ in config overrides the port settings. I always just switch them like I described. The docs are a bit fuzzy on that point. To find out for sure, set it up for debug with the ICSP inputs high and confirm it works. Then change RB7/6 to output 0 in your code, rebuild and see if it still debugs. If so, then leave the code that way. Otherwise use a debug/release switch.

    Good luck.
     
    Last edited: Jan 3, 2016
    R!f@@ likes this.
  6. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    8,750
    759
    Thanks John. :D
    I will take a whip at it tonight.
    I figured I need the "ANSELH" for 887 not for 886. :oops:

    I still got tons to learn about this. :(

    Still....
    887 has a lotta Analog ports but 886 has only 5. Why ANSELH ??
    Crap...! I checked just now and it has lot more than 5 Analog. Silly me.:oops:
    I will try ANSELH.
     
  7. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    8,750
    759
    Works like a Charm John, Thank you.
    ICSP works too. You were right about starting the debugger.
    Managed to get the LCD startup info and buzzer working.
    I believe I would need to use ur timer method to make delays. But for now I will try to finish the whole code with delays. If it works without using up the PIC space. OK
    If not I might need a help in figuring out multiple delays using the timers.
     
    JohnInTX likes this.
  8. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    8,750
    759
    I knew I would run into issues if I started to use LCD and delays.
    I am not even at 10% and the LCD is giving jumbled characters. I have had issues before on another project and it is sitting there today.
    @JohnInTX
    So I thought why not try the approach John showed in the Led strip project.
    So here I am asking for the best approach.
    I thought of this.
    Use interrupt to drive buzzer as before, and provide the delays.
    Use the interrupt to Update LCD every 100ms.
    The delay part this time is a bit complicated as I need delays for 2 seconds but I believe there is a way even though I dunno yet.

    What do you think John ?
    I am going to look at the Led strip and use the same approach to use IRQ.
    I am a bit lost as these two PIC's are very different.
    Still before starting I asking if the approach is OK.

    A little details.
    The LCD displays 2 msg one after the other. Each for 2 secs. That is only at startup.
    A little of beeping goes on as before.
    (I did the above normally and it is eating up of 25% ROM I think or is it RAM. The LCD is 16X2 in 4 bit mode.)

    Then the program starts.
    It first does a system check.
    Check for supply levels and battery. Each using ADC.
    A fault will hold the program and will alarm.
    Results will be displayed in LCD.
     
  9. JohnInTX

    Moderator

    Jun 26, 2012
    2,341
    1,024
    The same as I always think... when I am found actually thinking, that is.
    As we did in those other things, the PWM dimmer and the controller with multiplexed LED displays, start with a system TIK generated by an interrupt-driven timer. I recommend TIMER2/PR2 because it generates interrupts without intervention/resetting the timer etc.
    Once that is done, break each task into its own function. If a task requires timing e.g. flash an LED then it gets its own timer derived from the system TIK. Each time its called it examines its timer to see if its time to change the LED state. If not, it exits immediately and waits for the next time it is called. If its time to change, it changes the LED, reloads the timer then exits immediately. Recall the way we did the various timed tasks in main of the PWM/LED dimmer.
    All other functions, ADC, LCD display, button debounce/input etc. similarly get their OWN little function or set of functions or state machine implemented with a switch statement depending on how complicated their task is. But they all conform to the rule - ALL timing is done using timers derived from the system TIK, never a dumb delay().
    One concession to the rules we can make for now is the LCD display. You probably want to use MikroC's libraries for that. Keep in mind, though, that those routines are 'blocking' i.e. they will dumb_delay as required. You probably can get away with those for now if all of the other stuff is non-blocking. It may be enough. If not, we can re-write the LCD stuff to conform. For now, don't worry about it.
     
  10. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    8,750
    759
    I thought the MikroC LCD routine is using delays as I see the memory getting used up. Was not sure.
    There only 3 buttons, (RESET, System Checking and Battery Change) So not used always. May be once in a blue moon.
    5 ADC, reading Voltage and puts a respective high output to the relays when needed.
    The LCD, Displaying Voltage or System operation or Fault.

    OK then. I will see if I can start up the TIMER2 on this baby.
    hmmm..TIMER0 is 8 bit, TIMER1 is 16bit and TIMER2 is 8 bit

    PS. What you said makes sense now on the problem I had. Too much LCD messages with mikroC routine is messing up the characters.

    That project was on hold since no one could provide a solution to me. You were not here then John.
    After this I will try to use this method and see if that works
     
    Last edited: Jan 6, 2016
  11. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    8,750
    759
    So I tried after looking tht led strip one.
    Like you said I tried with TMR2 it was tricky as it was not located like 12F629 :oops:
    But I managed to write it & program. Nothing happened. No pee pee....soooo to show u, I tried, I saved the project as it were and changed the settings to TMR0 and saved as a separate name again . TMR0 is beeping. I am doing the beep first to make sure.
    When it beeped I knew it works and every time I press the reset it beeps.
    So I searched the TMR2 file and friggin' thing is saved as TMR0.. :eek: both file the same. I have no freakin' idea how that happened.
    So I am posting the beeping (TMRo) code
    Code (C):
    1. ********** PIC Configuration ************
    2.  
    3. --------------------------------------------------------------------------------
    4. ********** IO Configuration ************
    5. RA0 as AN0 - Senses Vbatt      -- Battery Voltage Monitor ADC in.
    6. RA1 as AN1 - Senses Charge_CS  -- Battery Charge Current Monitor ADC in.
    7. RA2 as AN2 - Senses MainPSU_OK -- Main Power Supply 18V to 19.5V Monitor ADC in.
    8. RA3 as AN3 - Senses SubPSU_OK  -- 13.5V PSU Monitor ADC in.
    9. RA4 not used.
    10. RA5 as AN4 - Senses Charger_OK -- Senses the Charger output Monitor ADC in.
    11. RA6 as Digital In - System check Switch - Active high.
    12. RA7 as Digital In - Battery Change Switch - Active high.
    13. RB0 as Digital Out - Active High output - Float Charging Signal.
    14. RB1 as Digital Out - Active High output - Charger Switch on Signal.
    15. RB2 not used.
    16. RB3 not used.
    17. RB4 as Digital In - Senses AC_OK -- Active High Signal.
    18. RB5 as Digital Out - Active High output - AC_ON Signal.
    19. RB6 as PGC for Debugging.
    20. RB7 as PGD for Debugging.
    21. RC0 to LCD RS
    22. RC1 to LCD Enable
    23. RC2 to LCD D4
    24. RC3 to LCD D5
    25. RC4 to LCD D6
    26. RC5 to LCD D7
    27. RC6 as Digital Output -- Buzzer Output
    28. RC7 as Digital Output -- LCD Backlit Enable.
    29. ------------------------------------------------------------------------------*/
    30. // ****** LCD Configuration ************
    31. sbit LCD_RS at RC0_bit;
    32. sbit LCD_EN at RC1_bit;
    33. sbit LCD_D4 at RC2_bit;
    34. sbit LCD_D5 at RC3_bit;
    35. sbit LCD_D6 at RC4_bit;
    36. sbit LCD_D7 at RC5_bit;
    37. sbit LCD_RS_Direction at TRISC0_bit;
    38. sbit LCD_EN_Direction at TRISC1_bit;
    39. sbit LCD_D4_Direction at TRISC2_bit;
    40. sbit LCD_D5_Direction at TRISC3_bit;
    41. sbit LCD_D6_Direction at TRISC4_bit;
    42. sbit LCD_D7_Direction at TRISC5_bit;
    43. // End of LCD Connections.
    44.  
    45. // Control bit Assignments.
    46. sbit Sys_Check at RA6_bit;
    47. sbit Batt_Change at RA7_bit;
    48. sbit Float_Charge at RB0_bit;
    49. sbit Charger_On at RB1_bit;
    50. sbit AC_OK at RB4_bit;
    51. sbit AC_On at RB5_bit;
    52. //sbit Buzzer at RC6_bit;
    53. sbit Backlit at RC7_bit;
    54. // Flags for above
    55. bit Sys_Ch, Batt_Ch, Float_Ch, AC__OK, AC__ON, Batt_Status, Check_Battery;
    56. bit SoundBEEPER;
    57. // End of Control bit Assignments.
    58.  
    59. // Variables
    60. char *Vout = "00.00";
    61. unsigned int Vbatt, VbattTemp;
    62. unsigned char msec5PreScaler, BeepTimer_5ms, PORTCing;
    63. /******* PROGRAM CONFIGURATION **********/
    64. #define Beepermask   0x40    // Buzzer @ RC6
    65. #define Backlitmask  0x80    // Baacklit @ RC7
    66. #define TMR0period_us 150
    67. #define TMR0reload 256-TMR0period_us
    68. #define msec5PSset 5000/TMR0period_us
    69.  
    70. // Beeper pattern. Bit mapped variable gets shifted right each BeepTIK.
    71. // If LSbit==1 beeper sounds for one BeepTIK.If 0, beeper is off for that TIK.
    72. // By scattering bits in BeepPattern, patterns and different beep times can be
    73. // generated.
    74. typedef unsigned int BeepPatternType;
    75. BeepPatternType BeepPattern;
    76. #define BeepCtrlmask ((BeepPatternType) 1)// tests LSbit in char or int or long
    77. #define BeepTIK_ms 25 // ms per tik - Beep duration
    78. #define BeepTimerSet (BeepTIK_ms / 5) //
    79. // Beeper patterns for int..commented the ones I do not use
    80. //#define Beep_OFF 0b0000000000000000 // no beep (copy/paste for new pattern)
    81. #define Beep_Short1 0b0000000000000001 // 1 short beep--->Switch press Normal
    82. #define Beep_Short2 0b000000000000101 // 2 short beeps-->Innitial On beep
    83. #define Beep_Long1 0b0000000011111111 // 1 long beep---->ON OFF beep
    84. //define Beep_Long2 0b0000111100001111 // 2 long beeps
    85. //#define Beep_Alarm 0b0101010101010101 // 8 short beeps
    86.  
    87.  
    88.  
    89. /********  ISR *************************/
    90. void interrupt(){
    91.     TMR0 += TMR0reload;
    92.     INTCON.T0IF = 0;                   // Clear TMR interrupt flag
    93.  
    94. //-------------Service Buzzer -----------/
    95.     if(SoundBEEPER)
    96.        PORTCing ^= Beepermask;
    97.       else
    98.        PORTCing &= ~Beepermask;
    99.        PORTC = PORTCing;
    100. //-------------Service Derived Timer ----/
    101. /*Prescaler counts msec5PSset interrupts to make a 5msec period.
    102.    From that, as many timers can be created as desired. Set the timer
    103.    to the number of IRQ periods to time. The timer will run to 0 and stop.
    104.     Poll the timer, when its 0, time is out. Do something, reload and go.*/
    105.     if(msec5PreScaler == 0){ // iff prescaler ran out..
    106.        msec5PreScaler = msec5PSset; // reset it and run timer(s)
    107.           if(BeepTimer_5ms) // If Beem timer is > 0
    108.              BeepTimer_5ms--; // decrement beep timer
    109.    }
    110.     else // else
    111.        msec5PreScaler--; // decrement 5 msec prescaler
    112. // IRQ service
    113. }
    114. /******** Tone *************************/
    115. void setBeepPattern(BeepPatternType p){
    116.       BeepPattern = p;
    117.       BeepTimer_5ms = 0;
    118. }
    119.  
    120. /******** Whole System Checking Routine ************/
    121.  
    122. void main(){
    123.     INTCON = 0;                        // Ckear IRQ's
    124.     PIE1 = 0;
    125.     PIE2 = 0;
    126.     OPTION_REG = 0x88;                 // No pullups
    127.  
    128.     ANSEL = 0x0F;                      // RA <0:4> as Analog Inputs
    129.     ANSELH = 0x00;                     // Rest of Analog as Digitla I/O
    130.     CM1CON0 = 0x07;                    // Disable comparator 1
    131.     CM2CON0 = 0x07;                    // Disable comparator 2
    132.  
    133.     PORTA = 0;                         // Clear Ports
    134.     PORTB = 0;                         // Clear Ports
    135.     PORTCing = 0;                      // Clear PORT C shadow
    136.     PORTC = 0;                         // Clear Ports
    137.     TRISA = 0xFF;                      // PortA as Inputs
    138.     TRISB = 0xDC;                      // PortB 'b11011100'
    139.     TRISC = 0x00;                      // PortC as Outputs
    140.  
    141.     Sys_Ch = 0;                        // Clear Flag bit
    142.     Batt_Ch = 0;                       // Clear Flag bit
    143.     Float_Ch = 0;                      // Clear Flag bit
    144.     AC__OK = 0;                        // Clear Flag bit
    145.     AC__ON = 0;                        // Clear Flag bit
    146.     Batt_Status = 0;                   // Clear Battery Status flag
    147.     Check_Battery = 0;
    148.     soundBEEPER = 0;
    149.     msec5PreScaler = 0;
    150.     BeepTimer_5ms = 0;
    151. // -------- Start TMR2 IRG  ------------------
    152.  
    153.     TMR0 = 0;                          // Clear TMR0
    154.     INTCON.T0IF = 0;                   // Clear TMR0 interrupt flag
    155.     INTCON.T0IE = 1;                   // Enable TMR0 Interrupt
    156.     INTCON.GIE = 1;                    // Enable Global Interrupt
    157.     Backlit = 1;
    158.     setBeepPattern(Beep_Short2);
    159.  
    160.  
    161.    while(1){                             // While
    162. //------------------- GENERATE BEEP PATTERN --------------------
    163. // Shifts BeepPattern each time period turning beeper on/off in pattern
    164.           if(BeepTimer_5ms == 0){ // time to do something?
    165.           if(BeepPattern != 0){ // more to do?
    166.           if(BeepPattern & BeepCtrlmask) // yes, beep on/off according
    167.           // to bit in pattern
    168.           soundBEEPER = 1; // Beep
    169.           else
    170.           soundBEEPER = 0; // Else don't beep
    171.           BeepTimer_5ms = BeepTimerSet; // then reload timer
    172.           BeepPattern = BeepPattern >> 1; // shift it right for
    173.           } // next pattern bit
    174.           else
    175.           soundBEEPER = 0; // no bits in pattern, turn it off
    176. }
    177. }
    178. }
    See the " Backlit output" is at RC7, buzzer at RC6
    With the TMR2 version no sound but LCD backlit turn on.
    With TMR0 version sound is OK but backlit flickers and goes off.

    I like to know how to setup the TMR2. I think it is located in the PIR register. Or am I wrong.:(
    I like to know cause you told me to use the TMR2 :D
     
    Last edited: Jan 6, 2016
  12. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    8,750
    759
    My PIC is set at 8Mhz.
    TMR0period is at 150us
    I am trying to figure out the buzzer Freq. This part still escapes my head
     
  13. JohnInTX

    Moderator

    Jun 26, 2012
    2,341
    1,024
    If TMR0period is 150us, then the buzzer period is 2*that = 300us and the freq is therefore 3333Hz because the raw drive to the beeper is just toggling the buzzer output at the interrupt rate. IIRC, you picked that for max beep sound, yes? If so that's our basic interrupt rate.
    To make it go with Timer2, you'll have to of course disable the TMR0 interrupt stuff and enable TMR2's stuff, easy enough. TMR2 is a free running 8 bit timer that generates a period by comparing its count to PR2.
    At 8MHz, you have a 500ns Tcyc (period into the timer chain)... Dragging out the Lil Professor I get:
    PreScaler = 1
    PostScaler = 10
    PR2 = 30 (decimal)
    for 150usec period that will generate your buzzer freq. All other timers will be derived from this as we did before.
    To start, I would get that working (comment out the rest of the stuff for now), force the buzzer ON and drag out that fancy Rigol scope of yours to confirm that you have a 300us period into the buzzer (2* the interrupt rate). Once you get that, we'll deal with the other stuff..
    Meanwhile, I'll review that 'Easy'7 of yours.
    Best,
    RJO
     
  14. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    8,750
    759
    Hmmm ! I wonder how you would be sitting and thinking :D

    Just so that I got everything...!
    8Mhz = > 8/4 = 2Mhz (Tcyc) => 1/2MHz = 500ns period . Got it.

    So If I chose to change the buzzer freq, that will effect the IRQ timing, right ?

    Oki doki..let's see if I can do what you asked.
    Disable all TMR0 stuff and enable all TMR2 stuff. Force the buzzer on and RIGOL the bugger !! :oops: Err, Buzzer.:p
     
  15. JohnInTX

    Moderator

    Jun 26, 2012
    2,341
    1,024
    Yes, if the buzzer is to be driven as we did in your PWM dimmer project. BUT! Thinking now, unlike that PIC, the '886 has a hardware PWM. Personally, I'd use that - set it at 50% duty cycle at the freq desired then just turn the PWM on/off. The PWM uses TMR2 so the system TIK goes to one of the other timers and gets set at a more reasonable 1ms - 10ms.
    That way, you get more control over the buzzer (play tunes if you like!) without changing the basic IRQ system TIK.
    I'd do that.
     
  16. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    8,750
    759
    I like that but this is not a "Tunes one"
    I will try that in some other project. This I like to go on with just beeping..Louder than usual.
     
  17. JohnInTX

    Moderator

    Jun 26, 2012
    2,341
    1,024
    Fair enough, I was kind of joking anyway. But the point is still valid, if you can use the hardware PWM that relieves some constraints on the rest of the program by not having to interrupt as often. But on your Easy7, the buzzer can be driven by the RC2 (PWM output) pin. If you want, start with the way we did it before (150us interrupts) and if it becomes a problem, fire up the hardware PWM. I would use the original timer setup if you go with the fast interrupt method.. That way, you can migrate to the PWM/TMR2 with minimal rewrite of the system TIK stuff.
     
  18. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    8,750
    759
    Point taken. :)
    But the PCB is already made and the PIC is in the circuit. I am using the EasyPIC ICSP with the PCB ICSP header. Just bread board jumper cables. I am programing the PIC in the circuit.
    So the buzzer is set up at RC6 and if I changed that I need to change the PCB. Too much time you know :(

    I checked. The buzzer is pretty loud at 150us.
    Now I am trying to change it TMR2.
    Will take some time. :D
     
  19. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    8,750
    759
    I did it, I did it :D
    Rigol Capture coming up
     
    JohnInTX likes this.
  20. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    8,750
    759
    Code (C):
    1. /******* PROGRAM CONFIGURATION **********/
    2. #define Beepermask   0x40    // Buzzer @ RC6
    3. #define Backlitmask  0x80    // Baacklit @ RC7
    4. #define TMR0period_us 150
    5. #define TMR0reload 256-TMR0period_us
    6. #define msec5PSset 5000/TMR0period_us
    7.  
    8. // Beeper pattern. Bit mapped variable gets shifted right each BeepTIK.
    9. // If LSbit==1 beeper sounds for one BeepTIK.If 0, beeper is off for that TIK.
    10. // By scattering bits in BeepPattern, patterns and different beep times can be
    11. // generated.
    12. typedef unsigned int BeepPatternType;
    13. BeepPatternType BeepPattern;
    14. #define BeepCtrlmask ((BeepPatternType) 1)// tests LSbit in char or int or long
    15. #define BeepTIK_ms 25 // ms per tik - Beep duration
    16. #define BeepTimerSet (BeepTIK_ms / 5) //
    17. // Beeper patterns for int..commented the ones I do not use
    18. //#define Beep_OFF 0b0000000000000000 // no beep (copy/paste for new pattern)
    19. #define Beep_Short1 0b0000000000000001 // 1 short beep--->Switch press Normal
    20. #define Beep_Short2 0b000000000000101 // 2 short beeps-->Innitial On beep
    21. #define Beep_Long1 0b0000000011111111 // 1 long beep---->ON OFF beep
    22. //define Beep_Long2 0b0000111100001111 // 2 long beeps
    23. //#define Beep_Alarm 0b0101010101010101 // 8 short beeps
    24.  
    25.  
    26.  
    27. /********  ISR *************************/
    28. void interrupt(){
    29.     TMR0 += TMR0reload;
    30.     INTCON.T0IF = 0;                   // Clear TMR interrupt flag
    31.  
    32. //-------------Service Buzzer -----------/
    33.     if(SoundBEEPER)
    34.        PORTCing ^= Beepermask;
    35.       else
    36.        PORTCing &= ~Beepermask;
    37.        PORTC = PORTCing;
    38. //-------------Service Derived Timer ----/
    39. /*Prescaler counts msec5PSset interrupts to make a 5msec period.
    40.    From that, as many timers can be created as desired. Set the timer
    41.    to the number of IRQ periods to time. The timer will run to 0 and stop.
    42.     Poll the timer, when its 0, time is out. Do something, reload and go.*/
    43.     if(msec5PreScaler == 0){ // iff prescaler ran out..
    44.        msec5PreScaler = msec5PSset; // reset it and run timer(s)
    45.           if(BeepTimer_5ms) // If Beem timer is > 0
    46.              BeepTimer_5ms--; // decrement beep timer
    47.    }
    48.     else // else
    49.        msec5PreScaler--; // decrement 5 msec prescaler
    50. // IRQ service
    51. }
    52. /******** Tone *************************/
    53. void setBeepPattern(BeepPatternType p){
    54.       BeepPattern = p;
    55.       BeepTimer_5ms = 0;
    56. }
    57.  
    58. /******** Whole System Checking Routine ************/
    59.  
    60. void main(){
    61.     INTCON = 0;                        // Ckear IRQ's
    62.     PIE1 = 0;
    63.     PIE2 = 0;
    64.     OPTION_REG = 0x88;                 // No pullups TMRO runs as Tcyc
    65.  
    66.     ANSEL = 0x0F;                      // RA <0:4> as Analog Inputs
    67.     ANSELH = 0x00;                     // Rest of Analog as Digitla I/O
    68.     CM1CON0 = 0x07;                    // Disable comparator 1
    69.     CM2CON0 = 0x07;                    // Disable comparator 2
    70.  
    71.     PORTA = 0;                         // Clear Ports
    72.     PORTB = 0;                         // Clear Ports
    73.     PORTCing = 0;                      // Clear PORT C shadow
    74.     PORTC = 0;                         // Clear Ports
    75.     TRISA = 0xFF;                      // PortA as Inputs
    76.     TRISB = 0xDC;                      // PortB 'b11011100'
    77.     TRISC = 0x00;                      // PortC as Outputs
    78.  
    79.     Sys_Ch = 0;                        // Clear Flag bit
    80.     Batt_Ch = 0;                       // Clear Flag bit
    81.     Float_Ch = 0;                      // Clear Flag bit
    82.     AC__OK = 0;                        // Clear Flag bit
    83.     AC__ON = 0;                        // Clear Flag bit
    84.     Batt_Status = 0;                   // Clear Battery Status flag
    85.     Check_Battery = 0;
    86.     soundBEEPER = 0;
    87.     msec5PreScaler = 0;
    88.     BeepTimer_5ms = 0;
    89. // -------- Start TMR0 IRG  ------------------
    90.     TMR0 = 0;                          // Clear TMR0
    91.     INTCON.T0IF = 0;                   // Clear TMR0 interrupt flag
    92.     INTCON.T0IE = 1;                   // Enable TMR0 Interrupt
    93.     INTCON.GIE = 1;                    // Enable Global Interrupt
    94.     Backlit = 1;
    95.     setBeepPattern(Beep_Long1);
    96.  
    97.  
    98.    while(1){                             // While
    99. //------------------- GENERATE BEEP PATTERN --------------------
    100. // Shifts BeepPattern each time period turning beeper on/off in pattern
    101.           if(BeepTimer_5ms == 0){ // time to do something?
    102.           if(BeepPattern != 0){ // more to do?
    103.           if(BeepPattern & BeepCtrlmask) // yes, beep on/off according
    104.           // to bit in pattern
    105.           soundBEEPER = 1; // Beep
    106.           else
    107.           soundBEEPER = 0; // Else don't beep
    108.           BeepTimer_5ms = BeepTimerSet; // then reload timer
    109.           BeepPattern = BeepPattern >> 1; // shift it right for
    110.           } // next pattern bit
    111.           else
    112.           soundBEEPER = 1; // no bits in pattern, turn it off
    113. }
    114. }
    115. }
    TMR0.png

    The above Image is for the TMR0 code as above.
    Below code and image is for TMR2

    Code (C):
    1. /******* PROGRAM CONFIGURATION **********/
    2. #define Beepermask   0x40    // Buzzer @ RC6
    3. #define Backlitmask  0x80    // Baacklit @ RC7
    4. #define TMR2period_us 150
    5. #define TMR2reload 256-TMR2period_us
    6. #define msec5PSset 5000/TMR2period_us
    7.  
    8. // Beeper pattern. Bit mapped variable gets shifted right each BeepTIK.
    9. // If LSbit==1 beeper sounds for one BeepTIK.If 0, beeper is off for that TIK.
    10. // By scattering bits in BeepPattern, patterns and different beep times can be
    11. // generated.
    12. typedef unsigned int BeepPatternType;
    13. BeepPatternType BeepPattern;
    14. #define BeepCtrlmask ((BeepPatternType) 1)// tests LSbit in char or int or long
    15. #define BeepTIK_ms 25 // ms per tik - Beep duration
    16. #define BeepTimerSet (BeepTIK_ms / 5) //
    17. // Beeper patterns for int..commented the ones I do not use
    18. //#define Beep_OFF 0b0000000000000000 // no beep (copy/paste for new pattern)
    19. #define Beep_Short1 0b0000000000000001 // 1 short beep--->Switch press Normal
    20. #define Beep_Short2 0b000000000000101 // 2 short beeps-->Innitial On beep
    21. #define Beep_Long1 0b0000000011111111 // 1 long beep---->ON OFF beep
    22. //define Beep_Long2 0b0000111100001111 // 2 long beeps
    23. //#define Beep_Alarm 0b0101010101010101 // 8 short beeps
    24.  
    25.  
    26.  
    27. /********  ISR *************************/
    28. void interrupt(){
    29.     TMR2 += TMR2reload;
    30.     PIR1.TMR2IF = 0;                   // Clear TMR interrupt flag
    31.  
    32. //-------------Service Buzzer -----------/
    33.     if(SoundBEEPER)
    34.        PORTCimg ^= Beepermask;
    35.       else
    36.        PORTCimg &= ~Beepermask;
    37.        PORTC = PORTCimg;
    38. //-------------Service Derived Timer ----/
    39. /*Prescaler counts msec5PSset interrupts to make a 5msec period.
    40.    From that, as many timers can be created as desired. Set the timer
    41.    to the number of IRQ periods to time. The timer will run to 0 and stop.
    42.     Poll the timer, when its 0, time is out. Do something, reload and go.*/
    43.     if(msec5PreScaler == 0){ // iff prescaler ran out..
    44.        msec5PreScaler = msec5PSset; // reset it and run timer(s)
    45.           if(BeepTimer_5ms) // If Beem timer is > 0
    46.              BeepTimer_5ms--; // decrement beep timer
    47.    }
    48.     else // else
    49.        msec5PreScaler--; // decrement 5 msec prescaler
    50. // IRQ service
    51. }
    52. /******** Tone *************************/
    53. void setBeepPattern(BeepPatternType p){
    54.       BeepPattern = p;
    55.       BeepTimer_5ms = 0;
    56. }
    57.  
    58. /******** Whole System Checking Routine ************/
    59.  
    60. void main(){
    61.     INTCON = 0;                        // Ckear IRQ's
    62.     PIE1 = 0;
    63.     PIE2 = 0;
    64.     OPTION_REG = 0x80;                 // No pullups TMRO runs as Tcyc
    65.  
    66.     ANSEL = 0x0F;                      // RA <0:4> as Analog Inputs
    67.     ANSELH = 0x00;                     // Rest of Analog as Digitla I/O
    68.     CM1CON0 = 0x07;                    // Disable comparator 1
    69.     CM2CON0 = 0x07;                    // Disable comparator 2
    70.  
    71.     PORTA = 0;                         // Clear Ports
    72.     PORTB = 0;                         // Clear Ports
    73.     PORTCimg = 0;                      // Clear PORT C shadow
    74.     PORTC = 0;                         // Clear Ports
    75.     TRISA = 0xFF;                      // PortA as Inputs
    76.     TRISB = 0xDC;                      // PortB 'b11011100'
    77.     TRISC = 0x00;                      // PortC as Outputs
    78.  
    79.     Sys_Ch = 0;                        // Clear Flag bit
    80.     Batt_Ch = 0;                       // Clear Flag bit
    81.     Float_Ch = 0;                      // Clear Flag bit
    82.     AC__OK = 0;                        // Clear Flag bit
    83.     AC__ON = 0;                        // Clear Flag bit
    84.     Batt_Status = 0;                   // Clear Battery Status flag
    85.     Check_Battery = 0;
    86.     soundBEEPER = 0;
    87.     msec5PreScaler = 0;
    88.     BeepTimer_5ms = 0;
    89. // -------- Disale TMR0 stuff  ------------------
    90.     INTCON.T0IF = 0;                   // Clear TMR0 interrupt flag
    91.     INTCON.T0IE = 0;                   // Disable TMR0 interrupt
    92. // -------- Enable TMR2 stuff  ------------------
    93.     TMR2 = 0;                          //Clear TMR2
    94.     PR2 = 0x1E;                        // PR2 is decimal 30
    95.     T2CON = 0x4C;                      // PostScaler = 1:10
    96.                                        // TMR2 ON
    97.                                        // Prescaler 1
    98.     PIR1.TMR2IF = 0;                   // Clear TMr2 to PR2 interrupt Flag
    99.     PIE1.TMR2IE = 1;                   // TMR2 to PR2 Match interrupt Enable
    100.     INTCON.PEIE = 1;                   // Enable all Pheripheral interrupt
    101.     INTCON.GIE = 1;                    // Enable Global interrupt
    102.  
    103.  
    104.     Backlit = 1;
    105.     setBeepPattern(Beep_Long1);
    106.  
    107.  
    108.    while(1){                             // While
    109. //------------------- GENERATE BEEP PATTERN --------------------
    110. // Shifts BeepPattern each time period turning beeper on/off in pattern
    111.           if(BeepTimer_5ms == 0){ // time to do something?
    112.           if(BeepPattern != 0){ // more to do?
    113.           if(BeepPattern & BeepCtrlmask) // yes, beep on/off according
    114.           // to bit in pattern
    115.           soundBEEPER = 1; // Beep
    116.           else
    117.           soundBEEPER = 0; // Else don't beep
    118.           BeepTimer_5ms = BeepTimerSet; // then reload timer
    119.           BeepPattern = BeepPattern >> 1; // shift it right for
    120.           } // next pattern bit
    121.           else
    122.           soundBEEPER = 1; // no bits in pattern, turn it off
    123. }
    124. }
    125. }
    TMR2.png

    Sound is Low in volume and tone.

    So I need to change something. I would try the 150us but I think that is not what you would do John.

    can you please go thru the TMR2 code and tell me if I should omit anything ?
     
    Last edited: Jan 7, 2016
Loading...