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();
}
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%)
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.
but not work with oshon soft ....>>> Warning 203 "main.c" Line 76(1,1): Condition always TRUE
Memory usage: ROM=53% RAM=17% - 30%
0 Errors, 1 Warnings.
/* 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
}
}
/*****************************************************************************
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
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 ?
not clear me (cont understand). if u don't mind pl me a give exampleThe 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.
// 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);
if (ch<=600) && (SW2=1)
//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);
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.
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
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?