• AVR Freaks

AnsweredHot!Simple LED Switch gone bad

Author
medinaev
New Member
  • Total Posts : 5
  • Reward points : 0
  • Joined: 2019/07/02 12:34:02
  • Location: 0
  • Status: offline
2019/07/04 19:01:54 (permalink)
0

Simple LED Switch gone bad

Hi, Im very new to the microchip development tool and im wondering 
why the c code i write on the ide is not working like it should
 
 
#include <xc.h>

#define _XTAL_FREQ 20000000
 
void main() {

TRISC = 0b00000001;

if(PORTCbits.RC0== 1)
PORTCbits.RC1 = 1;
else
PORTCbits.RC1 = 0;
 
i want to switch an led on and off every time i connect a 5 volt analog singal into the RC0
 
however RC1 is always on high and for some reason so is RB5.
 
what am i missing?
 
I am using the PIC16F687
 
 
 
post edited by medinaev - 2019/07/08 18:04:11
#1
katela
Super Member
  • Total Posts : 1330
  • Reward points : 0
  • Joined: 2013/06/11 05:25:18
  • Location: South Africa
  • Status: offline
Re: Simple LED Switch gone bad 2019/07/08 11:21:53 (permalink)
+1 (1)
So many things could be wrong.
First you are setting your PortC as output but want to read from it. Set RC0 as digital input.
How did you set your configuration bits?

Free online Microcontroller Tutorials and Projects for Hobbyists and students. From beginners to advanced. Website: www.studentcompanion.co.za
YouTube Tutorials: https://www.youtube.com/StudentCompanionSA
#2
PStechPaul
Super Member
  • Total Posts : 2350
  • Reward points : 0
  • Joined: 2006/06/27 16:11:32
  • Location: Cockeysville, MD, USA
  • Status: offline
Re: Simple LED Switch gone bad 2019/07/08 11:58:42 (permalink) ☼ Best Answerby medinaev 2019/07/14 18:48:58
+3 (3)
In addition to katela's observations:
 
1. Your main() function does not have a closing brace ( } )
2. You need an endless loop ( while(1) {code} ) to run the code
3. You need to reset the appropriate bit of ANSEL register to make the pin RC0 digital
 
See datasheet: http://ww1.microchip.com/...eviceDoc/40001262F.pdf page 59
post edited by PStechPaul - 2019/07/08 12:01:44

 
#3
ric
Super Member
  • Total Posts : 23232
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: Simple LED Switch gone bad 2019/07/08 13:40:24 (permalink) ☄ Helpfulby medinaev 2019/07/14 18:48:48
+1 (1)
#include <xc.h>

Good, you need this.
However, where are your CONFIG settings? These are important, and should be shown.
 
#define _XTAL_FREQ 20000000

Is your PIC really running at 20MHz?
You do NOT need this line until you use some library calls to __delay_us() or __delay_ms()
 
void main() {

This should be void main(void)
 
TRISC = 0b00000000;

This sets ALL of the PORTC pins to be outputs, so you can't use RC0 as an input after this.
 
if(PORTCbits.RC0== 1)

This does a DIGITAL read of RC0. You said it is an analog voltage. Does that mean it might something between 1V and 4V, or will it always be a valid digital level?
As Paul mentioned, analog capable pins power up in analog mode. You cannot do a digital read until you switch the pin to digital mode via the ANSEL register.
If you want to do an analog read, then you have to use the ADC peripheral to measure the actual voltage, you can't just read the PORT register.
 
PORTCbits.RC1 = 1;

Be aware that on old pics like the PIC16F687, this sort of instruction MIGHT affect the other 7 bits in PORTC.
This is the "read-modify-write" problem.
It won't cause today's problem, but will become an issue when you start using more pins on the same port.
 

however RC1 is always on high and for some reason so is RB5.

You have done nothing to set the state of pin RB5, so it is probably still in the power up default condition as an INPUT.
PORTB pins do have an internal pullup capability in this PIC, but it is turned off by default after power on.
How are you determining that RB5 is high?
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#4
Jump to:
© 2019 APG vNext Commercial Version 4.5