CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

A/D conversion

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
zaleha



Joined: 06 Apr 2008
Posts: 8

View user's profile Send private message

A/D conversion
PostPosted: Sun Apr 06, 2008 12:13 am     Reply with quote

hi all..
i'm am a new user for the microcontroller and have a very basic knowledge on a souce code. I am now working for my final project that use the 16F876A microcontroller and use a CCS as a compiler.My project is based on the ranging concept. The ADC should take the changing of voltage from the circuit and display the range through the LEDs. My problem now is when I running the circuit with the PIC, there is no output at all plus no voltage at the output pin. It is deal with my hardware or to my source code?
Hope you guys can help me... Here is my source code:

Quote:

//Initialize the PIC
#include <16F876.h>
#fuses HS
#fuses NOWDT
#fuses NOPROTECT
#fuses NOLVP
#fuses NODEBUG
#fuses NOPUT
#fuses NOBROWNOUT
#use delay(clock=4000000)
#use rs232 (baud=2400, parity=N, rcv=PIN_A1, bits=8)
#use fast_io(A)
#use fast_io(B)
#use fast_io(C)

//set i/o pins
#define MY_TRISA 0b00000011 // A7 - A3 = output,A2-A0=input
#define MY_TRISB 0b00000000 // B7 - B0 = output
#define MY_TRISC 0b00000000 // C7 -C0 = output
//distance definition
#define LED_DISTANCE1 PIN_B7 // out (green)
#define LED_DISTANCE2 PIN_B6 // out (green)
#define LED_DISTANCE3 PIN_B5 // out (green)
#define LED_DISTANCE4 PIN_B4 // out (yellow)
#define LED_DISTANCE5 PIN_B3 // out (yellow)
#define LED_DISTANCE6 PIN_B2 // out (red)
#define LED_DISTANCE7 PIN_B1 // out (red)
#define LED_DISTANCE8 PIN_B0 // out (red)
#define BUZZER PIN_C4 // out
//Rx pin definition
#define RX_RSSI PIN_A0 // in
#define RX_CS0 PIN_A2 // out
#define RX_CS1 PIN_A3 // out
#define RX_CS2 PIN_A5 // out

//global variables
int1 rx_cs_bit0;
int1 rx_cs_bit1;
int1 rx_cs_bit2;
//declarations
void initADC(void);
void initRX_parallel(void);

//functions

void initADC(void)
{
setup_adc_ports(RA0_ANALOG); // A0 A1 Ref=A2,A3
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel( 0 );
delay_us(10);
}
void initRX_parallel(void)
{
if (1 == rx_cs_bit0)
{
output_high(RX_CS0);
} else
{
output_low(RX_CS0);
}
if (1 == rx_cs_bit1)
{
output_high(RX_CS1);
} else
{
output_low(RX_CS1);
}
if (1 == rx_cs_bit2)
{
output_high(RX_CS2);
} else
{
output_low(RX_CS2);
}
}
#pragma zero_ram
void main(void)
{
// Variable Declaration
int8 rx_strength = 0;


set_tris_a(MY_TRISA);
set_tris_b(MY_TRISB);
set_tris_c(MY_TRISC);
disable_interrupts(GLOBAL);

delay_ms(500);
// Channel Select Vars for RX (channel 0: 000)
rx_cs_bit0 = 0;
rx_cs_bit1 = 0;
rx_cs_bit2 = 0;

// Initialize RX parallel, A/D Converter

delay_ms(50);
enable_interrupts(GLOBAL);
initADC();
initRX_parallel();

// initially no LEDs should be on
output_low(LED_DISTANCE8);
output_low(LED_DISTANCE7);
output_low(LED_DISTANCE6);
output_low(LED_DISTANCE5);
output_low(LED_DISTANCE4);
output_low(LED_DISTANCE3);
output_low(LED_DISTANCE2);
output_low(LED_DISTANCE1);
output_low(BUZZER);

// Read in data from receiver
while (1)
{
//A/D Conversion
rx_strength = read_adc();
// Light proper LEDs based on RSSI input
if (( rx_strength >= 0x33) && ( rx_strength<= 0x41)) // one red
{
output_high(LED_DISTANCE8);
output_low(LED_DISTANCE7);
output_low(LED_DISTANCE6);
output_low(LED_DISTANCE5);
output_low(LED_DISTANCE4);
output_low(LED_DISTANCE3);
output_low(LED_DISTANCE2);
output_low(LED_DISTANCE1);
}
else if (( rx_strength>= 0x42) && ( rx_strength <= 0x4b)) // two reds
{
output_high(LED_DISTANCE8);
output_high(LED_DISTANCE7);
output_low(LED_DISTANCE6);
output_low(LED_DISTANCE5);
output_low(LED_DISTANCE4);
output_low(LED_DISTANCE3);
output_low(LED_DISTANCE2);
output_low(LED_DISTANCE1);
}
else if (( rx_strength >= 0x4f) && ( rx_strength <= 0x56)) // 3 reds
{
output_high(LED_DISTANCE8);
output_high(LED_DISTANCE7);
output_high(LED_DISTANCE6);
output_low(LED_DISTANCE5);
output_low(LED_DISTANCE4);
output_low(LED_DISTANCE3);
output_low(LED_DISTANCE2);
output_low(LED_DISTANCE1);
}
else if (( rx_strength >= 0x57) && ( rx_strength <= 0x61)) // 3 reds, 1 yellow
{
output_high(LED_DISTANCE8);
output_high(LED_DISTANCE7);
output_high(LED_DISTANCE6);
output_high(LED_DISTANCE5);
output_low(LED_DISTANCE4);
output_low(LED_DISTANCE3);
output_low(LED_DISTANCE2);
output_low(LED_DISTANCE1);
}
else if (( rx_strength >= 0x65) && ( rx_strength <= 0x6b)) // 3 reds, 2 yellows
{
output_high(LED_DISTANCE8);
output_high(LED_DISTANCE7);
output_high(LED_DISTANCE6);
output_high(LED_DISTANCE5);
output_high(LED_DISTANCE4);
output_low(LED_DISTANCE3);
output_low(LED_DISTANCE2);
output_low(LED_DISTANCE1);
}
else if (( rx_strength >= 0x6f) && ( rx_strength <= 0x76)) // 3 reds, 2 yellows, 1 green
{
output_high(LED_DISTANCE8);
output_high(LED_DISTANCE7);
output_high(LED_DISTANCE6);
output_high(LED_DISTANCE5);
output_high(LED_DISTANCE4);
output_high(LED_DISTANCE3);
output_low(LED_DISTANCE2);
output_low(LED_DISTANCE1);
}
else if (( rx_strength >= 0x7a) && ( rx_strength <= 0x85)) // 3 reds, 2 yellows, 2 greens
{
output_high(LED_DISTANCE8);
output_high(LED_DISTANCE7);
output_high(LED_DISTANCE6);
output_high(LED_DISTANCE5);
output_high(LED_DISTANCE4);
output_high(LED_DISTANCE3);
output_high(LED_DISTANCE2);
output_low(LED_DISTANCE1);
}
else if ( rx_strength >= 0x8c) // 3 reds, 2 yellows, 3 greens
{
output_high(LED_DISTANCE8);
output_high(LED_DISTANCE7);
output_high(LED_DISTANCE6);
output_high(LED_DISTANCE5);
output_high(LED_DISTANCE4);
output_high(LED_DISTANCE3);
output_high(LED_DISTANCE2);
output_high(LED_DISTANCE1);
}
}

}
Ttelmah
Guest







PostPosted: Sun Apr 06, 2008 2:24 am     Reply with quote

First, prove that your chip is actually running. Write a tiny program, to just flash one LED.
Second, I'd guess that the problem is the classic PIC one of RMW. How are your LED's connected?.
Do a search here on this term, which will find dozens of threads and the problem described.
Third, as an aside, why not just output the entire portb byte, in one go?. Much smaller code, simpler to see what you are getting. Also, use the 'code' buttons, rather than 'quote' buttons for posting code. So:
Code:

#define ONE_RED (0b1)
#define TWO_REDS (0b11)
#define THREE_REDS (0b111)
//etc.

if (( rx_strength >= 0x33) && ( rx_strength<= 0x41)) // one red
   output_b(ONE_RED);
else if (( rx_strength>= 0x42) && ( rx_strength <= 0x4b)) // two reds
   output_b(TWO_REDS);
else if (( rx_strength >= 0x4f) && ( rx_strength <= 0x56)) // 3 reds
   output_b(THREE_REDS);

//etc.

This would actually allow the code to work, even with the RMW problem present, but you should fix this as well, since I'd suspect you are overloading the chip's output pins.

Best Wishes
zaleha



Joined: 06 Apr 2008
Posts: 8

View user's profile Send private message

Working with A/D conversion
PostPosted: Tue Apr 08, 2008 9:16 am     Reply with quote

I had tried the given codes. what happens is the LED lit forever and never follow the range. It seems that the LED lit up by using a supplied voltage from the PIC. What should I do?
Please anyone help me!!! any comments will be appreciated:cry:

My project is about tracking circuit. The receiver has a receiver strength signal indicator(RSSI) which will vary the voltage from 0.9V to 2.6 V due to the distance between receiver and transmitter. I am connecting the RSSI pin at the receiver to the ADC pin (A0) at the PIC 16F876A. In addition, I'm also make a function for a PIC choosing a channel for the receiver (A2,A3 and A5).

My problem is the LED is always lit up and never follow the instruction which it should be lit according to the range changing.

Notice that the channel selection been done perfectly because there is a transmitting and receiving action although there is no output at the PIC. My source code is as previous. My transmitter does not using any PIC to control it. Can anyone review my work and tell me where I'm wrong????? Thank you...
Ttelmah
Guest







PostPosted: Tue Apr 08, 2008 10:01 am     Reply with quote

Did you do the search on RMW?.
If the LED's are 'lit up by using a supplied voltage from the PIC', you _must_ add series resistors to limit the current.

Best Wishes
zaleha



Joined: 06 Apr 2008
Posts: 8

View user's profile Send private message

really need a helps here
PostPosted: Thu Apr 10, 2008 3:10 pm     Reply with quote

I had connected the series resistor to the LED with various ohmic value starts from 1k to 4.2k. What happened is yes the Led does not lit up but it still not perform the ranging function as defined in the source code.
Is there anyone here can helps me review my source code and tell me where I'm wrong and how should I write a program to let the PIC perform the output at the LED as it supposed to.
I have a very limited basic knowledge on all these matter and forgive me if I do any mistake. I realy need a helps in order to solve this proble as soon as possible.
zaleha



Joined: 06 Apr 2008
Posts: 8

View user's profile Send private message

really needs helps here
PostPosted: Thu Apr 10, 2008 3:16 pm     Reply with quote

In addition to previous problem, After I programmed the PIC using a previous source code back, the things become worse. there is no channel selection done by the PIC and the channel select had been done ferfectly before. And when I measure the voltage at the output pin, it gives 0v and the crystal also gives 0v or sometimes gives 0.93v. is that means that i should aware with the hardware problems? I'm also had test a PIC it self and it running as it supposed to. This means that the PIC was in a good condition right?
How should I write a right program in order to display ranging result??????? Crying or Very sad
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Apr 10, 2008 3:26 pm     Reply with quote

Make a little test program that just writes to the LEDs. To save time
I'm going to do it for you.

This program turns on half the LEDs, then it delays 1/2 second, then
it turns on the other half and does another delay. It does this continuosly.
This will prove if your LED circuit is working, at least somewhat.
There may still be problems if the current through each LED is set at
too high of a level, due to the choice of your series resistor values.
In that case, if you turned on all of the LEDs at the same time, the
PIC might reset. Also, the power supply might droop and cause
the PIC to reset.

This is the best way to design a project. Prove that each piece of it
is working, before proceeding to the next stage of the design. In this
step, you want to prove that your whole LED circuit is functional.
Try this program and see if it works.
Code:

#include <16F876.h>   
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)

//==========================
void main()
{

while(1)
  {
   output_b(0x55);
   delay_ms(500);

   output_b(0xAA);
   delay_ms(500);
  }

}
zaleha



Joined: 06 Apr 2008
Posts: 8

View user's profile Send private message

Help me pelase..
PostPosted: Thu Apr 10, 2008 10:58 pm     Reply with quote

I had test the given codes with the pic and LED. it doesn't work at all. The link blink only for a second and gone off and never lit again. although the PIC being reset. What is the right circuit configuration to test this leds?
I just connect the VDD and VSS to the positve and ground supply with all the LEDs at the port b. Also includes the crystal at the pin 13. Am i doing right thing?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Apr 10, 2008 11:35 pm     Reply with quote

Quote:

I am now working for my final project that use the 16F876A microcontroller.
I just connect the VDD and VSS to the positve and ground supply with all
the LEDs at the port b. Also includes the crystal at the pin 13. Am i doing
right thing?

Pin 13 is not one of the crystal pins on a 16F876A.
It has to be connected to pins 9 and 10. It also needs two
22 pf ceramic capacitors in the crystal circuit.

You also need a pull-up resistor on the MCLR pin.
Use 10K for the ICD2, or 47K for the ICD-U40.

I think you need to buy a board that's already working,
or at least study the schematic of a factory-built board
and copy the connections.
zaleha



Joined: 06 Apr 2008
Posts: 8

View user's profile Send private message

re: LED blinking
PostPosted: Thu Apr 10, 2008 11:57 pm     Reply with quote

I'm sorry i do mistaken to mention crystal pin. yes it is 9 and 10. And with the reset connection, finally the LED blinking as expected. Thank you very much. I will try to write a program for ranging LED.
zaleha



Joined: 06 Apr 2008
Posts: 8

View user's profile Send private message

I made it!!!
PostPosted: Fri Apr 11, 2008 5:45 am     Reply with quote

thank you guys for all your helps. At last my circuit running as well as expected results.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group