Continue to Site

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.

  • 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.

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

Status
Not open for further replies.
Might have found the problem. Try the following:

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:
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
0 1 mikroCPIC1618.exe -MSF -DBG -pP16F676 -DL -O11111114 -fo8 -N"C:\Users\Public\code\test\test.mcppi" -SP"C:\Users\Public\Documents\Mikroelektronika\mikroC PRO for PIC\defs\" -SP"C:\Users\Public\Documents\Mikroelektronika\mikroC PRO for PIC\Uses\P16\" -SP"C:\Users\Public\code\test\" "test.c" "__Lib_Math.mcl" "__Lib_MathDouble.mcl" "__Lib_System.mcl" "__Lib_Delays.mcl" "__Lib_CType.mcl" "__Lib_CString.mcl" "__Lib_CStdlib.mcl" "__Lib_CMath.mcl" "__Lib_ADC_A_B.mcl" "__Lib_LcdConsts.mcl" "__Lib_Lcd.mcl"
0 1139 Available RAM: 48 [bytes], Available ROM: 1024 [bytes]
0 126 All files Preprocessed in 203 ms
0 122 Compilation Started test.c
281 123 Compiled Successfully test.c
0 127 All files Compiled in 79 ms
0 1144 Used RAM (bytes): 17 (35%) Free RAM (bytes): 31 (65%) Used RAM (bytes): 17 (35%) Free RAM (bytes): 31 (65%)
0 1144 Used ROM (program words): 1022 (100%) Free ROM (program words): 2 (0%) Used ROM (program words): 1022 (100%) Free ROM (program words): 2 (0%)
0 125 Project Linked Successfully test.mcppi
0 128 Linked in 125 ms
0 129 Project 'test.mcppi' completed: 454 ms
0 103 Finished successfully: 30 Aug 2011, 20:29:40 test.mcppi
 
0 1144 Used ROM (program words): 1022 (100%) Free ROM (program words): 2 (0%) Used ROM (program words): 1022 (100%) Free ROM (program words): 2 (0%)

It's at 100% ROM, which doesn't lend itself to additional code. The only library that I had checked was ADC.
 
Might have found the problem. Try the following:

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.

right-now OK..
>>> Warning 203 "main.c" Line 76(1,1): Condition always TRUE
Memory usage: ROM=53% RAM=17% - 30%
0 Errors, 1 Warnings.
but not work with oshon soft ....
 

Attachments

  • 676.PNG
    676.PNG
    81.7 KB · Views: 153
Last edited by a moderator:
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:
rmain1972 I complied the code you posted with mikroC and it didn't use but what i posted
Code:
/*        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
     }

}
 
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?
 
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:
/*****************************************************************************
 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
 

Attachments

  • test_676_CCS.c
    7.4 KB · Views: 128
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.
 
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:
/*****************************************************************************
 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

r u check it ?
 
r u check it ?

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.
 
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...
 
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.
 
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.
not clear me (cont understand). if u don't mind pl me a give example
Code:
                            // 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 ?
 
if (ch<=600) && (SW2=1)

If you want to test ch<=600 and SW2=1, you need to write this:
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.
 
Code:
 // 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.
 
Code:
 // 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.

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.
 
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.

hear my program, so you can see it in the full context.
 

Attachments

  • test_676_CCS.c
    7.9 KB · Views: 121
  • test_676_CCS_01.h
    377 bytes · Views: 117
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:
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.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top