Discussion in "PIC Microcontroller Discussion" started by    norms    Feb 16, 2008.
Sun Mar 02 2008, 11:54 am
#21
help needed urgently... why the code for my microcontroller wont detect a voltage? the leds don't turn on when i put 3vs as an analog input. everyone help me? and then the vb code for displaying the reading...
Sun Mar 02 2008, 08:05 pm
#22
i need to see the circuit atleast..
Mon Mar 03 2008, 01:34 am
#23
On close examination of your code,this is the logic I figured out.....

for(;;)
{
if(usart receive buffer is full)...read usart....volts_out=value read
{ if(volts_out=511)....light up some leds and loop till receive buffer is full again
elseif(volts_out<511)....light up some leds and loop till receive buffer is full again
elseif(volts_out>511)....light up some leds and loop till receive buffer is full again
elseif(volts_out=get_adc)...read adc value,store it in EEPROM,read the stored
EEPROM value and send it to USART
}
else...PORTB=0xFF
}

Instead of the ADC you are reading in the value "Volts_out" from the USART....why?
Moreover you need to send in some value from the PC to the USART in order for the 1st ' if' statement to trigger.(if(usart receive buffer is full))
And then the variable get_adc...you are comparing volts_out to it before providing it with an initial value(you've only declared it "unsigned int get_adc;",not initialised it).

Look at the code below...this is how it should be.

unsigned int adc_val;
unsigned char volts_out;

void main()
{
USART_Init(9600);
ADCON1 = 0x80;
TRISA = 0xFF; // PORTA is input
TRISB = 0; // PORTB is output
PORTB = 0;


while (1)
{
adc_val = ADC_Read(2);
volts_out = adc_val * 5/1023;
USART_Write(volts_out);
if(adc_val==511)
{PORTB = 0xFF;}
else if(adc_val<511)
{PORTB = 0x0F;}
else
{PORTB = 0xF0;}
Delay_ms(5000);
}

}
Mon Mar 03 2008, 01:16 pm
#24
at this moment my current code for my microcontroller 16F877, please correct it and write the code if you don't mind... please... really reallly need it.

void main()
{
int limit = 512;
int vout;
int adc;
int x;
int value;
int temp1;
int temp2;
int exit = 1;

Usart_Init(2400);
ADCON0 = 0x80;
ADCON1 = 0x80;
ADCON0.ADON = 1; // Enables A/D Conversion
TRISB = 0;
PORTB = 0;
RCSTA = 0x90;
TXSTA = 0x24;
Delay_ms(100);

for(;;)
{
if(Usart_Data_Ready())
{
exit=1;
PORTB=0;
vout=Usart_Read();
Delay_ms(100);

if(vout == 512) // Within the 240 Voltage Range, equal to 2.5 volts
{
do
{
PORTB.F1 = 1;
PORTB.F2 = 1;
PORTB.F3 = 1;
PORTB.F4 = 1;
PORTB.F5 = 1;
PORTB.F6 = 1;
PORTB.F7 = 1;
Delay_ms(1000);
if(Usart_Data_Ready())
{
exit=0;
}
}
while(exit);
}
else if(vout > 512) // Greater than 240 Volts, greater than 2.5 volts
{
do
{
PORTB.F1 = 1;
PORTB.F3 = 1;
PORTB.F5 = 1;
PORTB.F7 = 1;
Delay_ms(1000);
if(Usart_Data_Ready())
{
exit=0;
}
}
while(exit);
}
else if (vout < 512) // Less than 240 volts, less than 2.5 volts
{
do
{
PORTB.F2 = 0;
PORTB.F4 = 0;
PORTB.F6 = 0;
Delay_ms(1000);
if (Usart_Data_Ready())
{
exit = 0;
}
}
while(exit);
}
else if (vout == 0)
{
do
{
PORTB.F1=0;
PORTB.F2=0;
PORTB.F3=0;
PORTB.F4=0;
PORTB.F5=0;
PORTB.F6=0;
PORTB.F7=0;
if(Usart_Data_Ready())
{
exit=0;
}
}
while(exit);
}
else if(vout == adc)
{
do
{
ADRESH=0;
ADRESL=0;
ADCON0.GO=1;
Delay_ms(100);
value=Adc_Read(0);
Delay_ms(100);
exit=0;
temp1=ADRESH;
temp2=ADRESL;
Delay_ms(1000);
Eeprom_Write(0,temp1);
Delay_ms(20);
Eeprom_Write(1,temp2);
Delay_ms(70);
Usart_Write(Eeprom_Read(0));
Delay_ms(20);
Usart_Write(Eeprom_Read(1));
Delay_ms(50);
if(Usart_Data_Ready())
{
exit=0;
}
}
while(exit);
}
}
else
{
PORTB=0xff;
}
}
}
Mon Mar 03 2008, 01:18 pm
#25
my vb code... please check and correct it... thank you very much guys...

Private Sub CmdExit_Click()
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
End If
End
End Sub
Private Sub Form_Load()
MSComm1.InputLen = 0
MSComm1.CommPort = 1
MSComm1.Settings = "2400,N,8,1"
End Sub
Private Sub Timer1_Timer()
Dim BytesToRead As Integer
Dim adc As Variant
MSComm1.PortOpen = True
BytesToRead = 1
Do
DoEvents
Loop Until MSComm1.InBufferCount = BytesToRead
adc = MSComm1.Input
Text1.Text = (((Asc(adc) * 0.01) * 230) / 2.5) & " Volts AC"
MSComm1.PortOpen = False
End Sub
Mon Mar 03 2008, 01:39 pm
#26
this is my circuit

Mon Mar 03 2008, 02:02 pm
#27
the Tx and Rx were put incorrectly... Rx should in the Tx place...
Tue Mar 04 2008, 12:47 am
#28
Like I pointed out ealier your PIC code won't work.For eg.
if(Usart_Data_Ready()) 

why are you checking the (PIC's) serial port input buffer?Are you sending sth into the PIC's serial port?If not then why use this check?

Then you have
vout=Usart_Read();

You should be getting the value from the ADC rather than the serial port.

Similarly there are plenty of flaws.......

Look at the code below.This is all you need to read in ADC value and send it as two bytes to the PC's serial port.

unsigned int adc_val;
unsigned char low_byte,hi_byte;
void main()
{
USART_Init(9600);
ADCON1 = 0;
TRISA = 0xFF; // PORTA is input
TRISB = 0; // PORTB is output
PORTB = 0;

while (1)
{
adc_val = ADC_Read(2); //get ADC value
low_byte = adc_val; //split the 2-byte integer into 2 separate bytes
hi_byte = adc_val >> 8; //for sending to pc serial port
USART_Write(low_byte); //send the 1st (lower) byte
USART_Write(hi_byte); //send the upper byte
if(adc_val==511) //if ADC value read in is 511
{PORTB = 0xFF;} //display some LED's on PORTB
else if(adc_val<511)
{PORTB = 0x0F;}
else
{PORTB = 0xF0;}
Delay_ms(5000); //gather a sample after 5 secs.
}
}


[ Edited Tue Mar 04 2008, 12:48 am ]
Tue Mar 04 2008, 12:41 pm
#29
thanks sashijoseph for the help. appreciate it very much... ill test it tonight ill keep in touchwith you. you save my life...
Tue Mar 04 2008, 01:05 pm
#30


Like I pointed out ealier your PIC code won't work.For eg.

if(Usart_Data_Ready()) 

why are you checking the (PIC's) serial port input buffer?Are you sending sth into the PIC's serial port?If not then why use this check?

Then you have
vout=Usart_Read();

You should be getting the value from the ADC rather than the serial port.

Similarly there are plenty of flaws.......

Look at the code below.This is all you need to read in ADC value and send it as two bytes to the PC's serial port.

unsigned int adc_val;
unsigned char low_byte,hi_byte;
void main()
{
USART_Init(9600);
ADCON1 = 0;
TRISA = 0xFF; // PORTA is input
TRISB = 0; // PORTB is output
PORTB = 0;

while (1)
{
adc_val = ADC_Read(2); //get ADC value
low_byte = adc_val; //split the 2-byte integer into 2 separate bytes
hi_byte = adc_val >> 8; //for sending to pc serial port
USART_Write(low_byte); //send the 1st (lower) byte
USART_Write(hi_byte); //send the upper byte
if(adc_val==511) //if ADC value read in is 511
{PORTB = 0xFF;} //display some LED's on PORTB
else if(adc_val<511)
{PORTB = 0x0F;}
else
{PORTB = 0xF0;}
Delay_ms(5000); //gather a sample after 5 secs.
}
}

sashijoseph



what happened to the USART_Write(volts_out)? in your first correction you have it, then now its gone, replace by the low and high byte... please enlightened me with this...

then in my vb pro, do i need to change the BytesToRead = 1 to 2? and then the "adc=MSComm1.Input"
what will be my input here? thanks

Get Social

Information

Powered by e107 Forum System

Downloads

Comments

AntoniaRoons
Fri Apr 19 2024, 09:59 pm
carpinteyrowrl
Fri Apr 19 2024, 02:51 pm
DonaldJAX
Fri Apr 19 2024, 01:08 pm
Lewisuhakeply
Thu Apr 18 2024, 06:00 pm
Darrellciz
Thu Apr 18 2024, 11:07 am
Charlessber
Thu Apr 18 2024, 09:29 am
BartonSem
Thu Apr 18 2024, 04:56 am
DonaldKnown
Thu Apr 18 2024, 12:24 am