1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

PORTB =0b00111111; vs port bit (RB1_bit=0;)

Discussion in 'Microcontrollers' started by sahu, Aug 24, 2011.

  1. rmain1972

    rmain1972 Member

    Joined:
    Apr 13, 2011
    Messages:
    103
    Likes:
    2
    Location:
    Oklahoma City, OK USA
    Might have found the problem. Try the following:

    Code (c):

       int1 done;                      // Put local variable at beginning of code block

       set_adc_channel(0);        // Select AN0
       delay_ms(ADC_DELAY);       //Charge capacitor
       read_adc(ADC_START_ONLY);  //Do A/D conversion

       done =  = adc_done();

       while(!done) {
          done = adc_done();
       }
     
    Found this on the CCS Support forum:
    Variable Declaration - Position Dependant? - SOLVED

    If all else fails, try making it a global variable.
     
    Last edited by a moderator: Dec 26, 2013
  2. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,799
    Likes:
    134
    Location:
    morristown,tn
    ONLINE
    I was playing with this to day had some time you can build this for the 16f676 if you turn off some of the library's that are not needed
    and it dosn't use but
     
  3. rmain1972

    rmain1972 Member

    Joined:
    Apr 13, 2011
    Messages:
    103
    Likes:
    2
    Location:
    Oklahoma City, OK USA
    It's at 100% ROM, which doesn't lend itself to additional code. The only library that I had checked was ADC.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. sahu

    sahu Member

    Joined:
    Oct 18, 2009
    Messages:
    468
    Likes:
    3

    right-now OK..
    but not work with oshon soft ....
     

    Attached Files:

    • 676.PNG
      676.PNG
      File size:
      81.7 KB
      Views:
      81
    Last edited by a moderator: Dec 26, 2013
  6. rmain1972

    rmain1972 Member

    Joined:
    Apr 13, 2011
    Messages:
    103
    Likes:
    2
    Location:
    Oklahoma City, OK USA
    I'm not sure why its only running 5 instructions in Oshon Soft. I did notice that there are a couple of gotchas between converting MikroC to CCS. Namely the default data types are different. In MikroC, a int data type is 16 bits; In CCS, it is 8 bits. So I changed int to int16, and long to int32. I'm getting absurd numbers at 4.0V for AN0 in Oshon Soft. I'm looking into it. I'm going to try to use MPLAB SIM because I like the debugger better, but I'm not sure how to set AN0 in it, so I'm trying to do some research.
     
    Last edited: Aug 31, 2011
  7. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,799
    Likes:
    134
    Location:
    morristown,tn
    ONLINE
    rmain1972 I complied the code you posted with mikroC and it didn't use but what i posted
    Code (text):

    /*        16F676 Configuration
              O = Output, I = Input
                        _________
                  Vdd   | 1  14 |  Vss
      (I) SW1 --> RA5   | 2  13 |  AN0 <-- ADC_Value (I)
      (I) SW2 --> RA4   | 3  12 |  RA1 --> RL4 (O)
                 MCLR   | 4  11 |  RA2 --> RL5 (O)
      (O)  L1 <-- RC5   | 5  10 |  RC0 --> RL1 (O)
      (O)  L1 <-- RC4   | 6   9 |  RC1 --> RL2 (O)
      (O) BZR <-- RC3   | 7   8 |  RC2 --> RL3 (O)
                        ---------
    */

    /*................................................................................
     Delay Defines
    .................................................................................*/
    #define BLINK_DELAY 1           // Change to "1000" for one SEC for real card
    #define ADC_DELAY 100           // Change to "1" for 1 ms for real card

    /*................................................................................
     NORMAL_VOLTAGE / HIGH_VOLTAGE / LOW_VOLTAGE Defines
    .................................................................................*/
    #define NORMAL_VOLTAGE 0           // Return Value indicates Normal Voltage is present
    #define HIGH_VOLTAGE 1             // Return Value indicates High_Voltage is present
    #define LOW_VOLTAGE 2              // Return Value indicates Low_Voltage is present

    /*................................................. ...............................
     Global Variables
    .................................................. ...............................*/
    unsigned int  ADC_Value;
    long ch , tlong;
    int voltage_type;

    /*................................................. ...............................
     Define relay selection port pins
    .................................................. ...............................*/
    sbit RL1 at RC0_bit;
    sbit RL2 at RC1_bit;
    sbit RL3 at RC2_bit;
    sbit RL4 at RA1_bit;
    sbit RL5 at RA2_bit;

    /*................................................. ...............................
     Define LED/ bzr selection port pins
    .................................................. ...............................*/
    sbit L1  at  RC5_bit; //PIN 05 hi\lo cut LED
    sbit L2  at  RC4_bit; //PIN 06 mains normal LED
    sbit BZR at  RC3_bit; //PIN 07 SOUND

    /*................................................. ...............................
     Define LED/ bzr selection port pins
    .................................................. ...............................*/

    sbit SW1 at  RA5_bit;//PIN 03 hi\lo cut enabol
    sbit SW2 at  RA4_bit;//PIN 02 QEK SATART

    /*................................................................................
     Function prototypes
    .................................................................................*/
    int ProcessMains(void);
    void BlinkLED1(void);
    void BlinkLED2(void);

    void main()
    {
        TRISA = 0b00110001;        //AN0, RA4, RA5 Input, all others output
        TRISC = 0b00000000;        //All output
       
        // Configure ADCON0 for channel AN0
        ADCON1=0x00;   // FOsc/2
        ANSEL = 0x01;  // RA0 analog input as AN0, all others digital
        CMCON = 0x07 ;
         while(1)
         {
              voltage_type = ProcessMains();
             
              BlinkLED1();
              BlinkLED2();
             
              /* Below orginal code */
             
              //loop_01:
              //    L2 = 1;// mains normal on
              //  L1 = 0;// hi\lo cut off
              //BZR = 0;// SOUND off
              //RL4 = 0;// Output relay off R4
             
              /*................................................. ...............................
                Delay mode selection for user friendly...
                .................................................. ...............................*/

              //if(SW2)
              //{
              //    Delay_ms(5);
              //}
              //else
              //{
              //  Delay_ms(1);
              //}
       
              /* .................................................. .............................
                 Main working starts here
                 .................................................. ................................*/
                 
              // L1 = 0; // delay indicator off.


         }
    }  // End main()

    /* Function ProcessMains
       Reads ADC Value and process */
    int ProcessMains(void)
    {
         ADCON0=0b00000001; //Left justified, Channel AN0 selected, ADON On
         Delay_ms(ADC_DELAY) ; //Allow 1ms for holding cap to charge

         GO_DONE_bit=1; //Start A/D process

         while (GO_DONE_bit==1) //Wait for A/D to finish
         {
         }

         ADC_Value = ADC_Read(0); // A/D conversion. Pin RA0 is an input.
         tlong =(long)ADC_Value*5000; // Convert the result in millivolts
         tlong = tlong/1023; // 0..1023 -> 0-5000mV
         ch = tlong; // Extract volts (thousands of millivolts)

         /*................................................. ...............................
           Main stabilizing is done here
           .................................................. ...............................*/

         if((ch<=4000)&&(ch>=0675))
         {
              if(ch>1035)       //115v
              {
                   if(ch>1260)      //140v
                   {
                        if(ch>1503)       //167 v
                        {
                             if(ch>1800)      //200v
                             {
                                  if(ch>2160)    //240v
                                  {
                                       if(ch>2592)    //= 288vac
                                       {
                                            if(ch>=3115)      //= 346vac
                                            {
                                                 //PORTC =0b1111;
                                                 RL1 = 1;
                                                 RL2 = 1;
                                                 RL3 = 1;
                                                 RL4 = 1;
                                            }
                                            else
                                            {
                                            //PORTC =0b0111;
                                            RL1 = 1;
                                            RL2 = 1;
                                            RL3 = 1;
                                            RL4 = 0;
                                            }
                                       }
                                       else
                                       {
                                            //PORTC =0b1011;
                                            RL1 = 1;
                                            RL2 = 1;
                                            RL3 = 0;
                                            RL4 = 1;
                                       }
                                  }
                                  else
                                  {
                                       //PORTC =0b0011;
                                       RL1 = 1;
                                       RL2 = 1;
                                       RL3 = 0;
                                       RL4 = 0;
                                  }
                             }
                             else
                             {
                                  //PORTC =0b1001;
                                  RL1 = 1;
                                  RL2 = 0;
                                  RL3 = 0;
                                  RL4 = 1;
                             }
                        }
                        else
                        {
                             //PORTC =0b0001;
                             RL1 = 1;
                             RL2 = 0;
                             RL3 = 0;
                             RL4 = 0;
                        }
                   }
                   else
                   {
                        //PORTC =0b1000;
                        RL1 = 0;
                        RL2 = 0;
                        RL3 = 0;
                        RL4 = 1;
                   }
              }
              else
              {
                   //PORTC =0b0000;
                   RL1 = 0;
                   RL2 = 0;
                   RL3 = 0;
                   RL4 = 0;
              }

              /* Check this code, is this supposed to be RL4? */
              RL4 = 1; // output relay on.
               L2 = 1; // mains indicator on
               L1 = 0; // high //low voltage indicator off
              BZR = 0; // high //low voltage indicator off
         }
         
         /*................................................. ...............................
           High voltage protection for load is done here
           .................................................. ...............................*/

           if(ch > 2100)
           {
                L1 = 1; // high // low voltage indicator on
                BZR = 1; // cret sound
                RL4 = 0; // relay off
                L2 = 0; // Mains normal indicator off
                return(HIGH_VOLTAGE);  // Exit function    // goto loop_01;

           }

           /*................................................. ...............................
             Low voltage protection is done here
             .................................................. ...............................*/
           if(ch <= 1300)
           {
                L1 = 1; // high // low voltage indicator on
                BZR = 1; // cret sound
                RL4 = 0; // relay off
                L2 = 0; // Mains normal indicator off
                return(LOW_VOLTAGE); // Exit function        //  goto loop_01;
           }
           
           return(NORMAL_VOLTAGE);
    }

    /*................................................. ...............................
     Blink Mains LED -- Normal Mode
    .................................................. ...............................*/
    void BlinkLED1(void)
    {
         int x;

         for(x=0; x<=9; x++)  // Repeat 10 times, count from 0 to 9
         {
              L1 = 1;        // Turn on LED
              delay_ms(BLINK_DELAY);
              L1 = 0;        // Turn off LED
         }
         
    }

    void BlinkLED2(void)
    {
         int x;

         for(x=0; x<=9; x++)  // Repeat 10 times, count from 0 to 9
         {
              L2 = 1;        // Turn on LED
              delay_ms(BLINK_DELAY);
              L2 = 0;        // Turn off LED
         }

    }
     
     
  8. sahu

    sahu Member

    Joined:
    Oct 18, 2009
    Messages:
    468
    Likes:
    3
    Not clear ,what u want to say?
     
  9. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,160
    Likes:
    909
    Location:
    Rochdale UK
    Sahu... I run the code on oshonsoft (curious) the 4th instruction is "call 0x3ff"... On some processors this is a reset, but 0x3ff is the last location on the chip and should contain 0x00 (nop) and yours doesn't.... Did you change processors along the way?
     
  10. rmain1972

    rmain1972 Member

    Joined:
    Apr 13, 2011
    Messages:
    103
    Likes:
    2
    Location:
    Oklahoma City, OK USA
    It seems to be working in MPLAB SIM. I loaded the AD Result registers through a stimlus file with 0x15E (350 or 1.7V), 0x2BC (700 or 3.4V), and 0x332 (818 or 4.0V).

    The processor defaults to the A/D result as left justified. The CCS compiler does not change this. I had to change it to right justified by setting ADFM bit to 1. As well as change int to int16 and long to int32. MPLAB SIM also warned that conversion clock was less than 1.6 µs, so I changed to ADC_CLOCK_DIV_32. This was for a 676 running at 20 Mhz. I never asked at what speed your running your 676 at. In Oshon Soft, it looks like 4 Mhz. Is that correct?

    Code (text):


    /*****************************************************************************
     SFR Bit Defines
     ****************************************************************************/
    #bit ADFM = 0x1F.7          // A/D Result Formed Select bit
                                // 0=Left Justified, 1=Right Justified
    // Later in code, after  setup_adc(ADC_CLOCK_DIV_32);
     ADFM = 1;                          //AD Result Right Justified
     
     

    Attached Files:

  11. rmain1972

    rmain1972 Member

    Joined:
    Apr 13, 2011
    Messages:
    103
    Likes:
    2
    Location:
    Oklahoma City, OK USA
    be80be,

    If you look at your post #42, it says 100% ROM used with 2 program memory words left free. You can't add more code. Unless your quote is before you changed library settings, etc.
     
  12. sahu

    sahu Member

    Joined:
    Oct 18, 2009
    Messages:
    468
    Likes:
    3
    r u check it ?
     
  13. rmain1972

    rmain1972 Member

    Joined:
    Apr 13, 2011
    Messages:
    103
    Likes:
    2
    Location:
    Oklahoma City, OK USA
    I don't understand what you are trying to say. It runs in MPLAB SIM and Oshon Soft. You'll need to test it for the various voltage levels. My version still doesn't handle ch>4000. You'll have to handle that. Refer to post #25.
     
  14. sahu

    sahu Member

    Joined:
    Oct 18, 2009
    Messages:
    468
    Likes:
    3
    thank u my dear sir rmain1972
    now its working with OSHON SOFT sim.
    BlinkLED1 requirement only one time (means if we set it 10 times ,it Blink only 10 times not repeat all time & also if SW1 {RA5} on only 1 time in duration of 10 times BlinkLED1 ,its result=L1 & RL4 ON). SW1 we can say QUAKE START switch .

    hope understand what i am saying!!!

    very - very thank in advance...
     
  15. rmain1972

    rmain1972 Member

    Joined:
    Apr 13, 2011
    Messages:
    103
    Likes:
    2
    Location:
    Oklahoma City, OK USA
    I want you to try to code your requirements in #53 yourself.

    The BlinkLED1 requirement to only blink for a total of ten times could be done with another global variable as a flag. If it's set (ie, set to 1), skip running the blink code. You'd probably want to reset the variable (ie, set to 0) somewhere in code, possibly in the NORMAL_VOLTAGE switch case.

    I'm not sure what you mean about SW1. Let me try to clarify what you are saying a little. SW1 is a Quick Start button. If SW1 is on, turn on L1 and R4. I would probably put the test for SW1 (if statement) in the HIGH_VOLTAGE and LOW_VOLTAGE switch cases. If SW1=1, then turn on L1 and R4.

    If you need more help, just post what you have done, and then we will go from there. It's not that I don't want to help you, but the only way you are going to learn is by doing it yourself. So give it a try, we'll always be here to help if you get stumped.
     
  16. sahu

    sahu Member

    Joined:
    Oct 18, 2009
    Messages:
    468
    Likes:
    3
    not clear me (cont understand). if u don't mind pl me a give example
    Code (text):
                                // Low Voltage protection is done here
       if (ch<=600) && (SW2=1)  // Low Voltage protection enable  if SW2 all ways on
                             //if SW2 all ways off  Low Voltage protection disable
          return(LOW_VOLTAGE);
    i am right way ?
     
  17. rmain1972

    rmain1972 Member

    Joined:
    Apr 13, 2011
    Messages:
    103
    Likes:
    2
    Location:
    Oklahoma City, OK USA
    If you want to test ch<=600 and SW2=1, you need to write this:
    Code (c):

                                              //Low Voltage protection is done here
    if ( (ch<=600 && (SW2==1) )   //Low Voltage protection enable if SW2 is always on
         return(LOW_VOLTAGE);
     
    I'd probably add another return code, and return LOW_VOLTAGE_OVERRIDE to the switch case block.

    Watch your '=' and '==' signs. Notice the '==' sign in my code example. In C, '=' is for assignment, and '==' is for equality.
     
  18. sahu

    sahu Member

    Joined:
    Oct 18, 2009
    Messages:
    468
    Likes:
    3
    Code (text):
     // Low Voltage protection is done here
       if ((ch<=600) && (SW2==1)) //Low Voltage protection enable if SW2 is always on
          return(LOW_VOLTAGE);
    not working on OSHON SOFT.
     
  19. rmain1972

    rmain1972 Member

    Joined:
    Apr 13, 2011
    Messages:
    103
    Likes:
    2
    Location:
    Oklahoma City, OK USA
    Can you be more specific, what's not working. Please attach your program, so we can see it in the full context.

    Another thing you might want to consider is how you are going to debounce your switches. Hardware or Software? I know you are only simulating at this moment, but in the real world with out switch debounce your going to have times when the switch bounces between 1 and 0.
     
  20. sahu

    sahu Member

    Joined:
    Oct 18, 2009
    Messages:
    468
    Likes:
    3
    hear my program, so you can see it in the full context.
     

    Attached Files:

    • Like Like x 1
  21. rmain1972

    rmain1972 Member

    Joined:
    Apr 13, 2011
    Messages:
    103
    Likes:
    2
    Location:
    Oklahoma City, OK USA
    SW2 is a define, and it resolves directly to a number; therefore the compiler never created the code for the test. In order to read a pin in CCS, use the input() function.

    So your test code should be:
    Code (c):

     if ((ch<=674) && (input(SW2)==1)) //Low Voltage protection enable if SW2 is always on(conect with GND).
        return(LOW_VOLTAGE);  //if SW2 is always off(no conect with GND & +5 volt.) Low Voltage protection disable
     
    Took me a few minutes to figure this out. I would set a breakpoint in MPLAB SIM, and it would tell me that the 'breakpoint could not be resolved." The reason it could not be resolved was that it was being optimized out.
     

Share This Page