View previous topic :: View next topic |
Author |
Message |
Josh Davis Guest
|
Pin RB1 on 16f873 fails to go completely low. |
Posted: Sun Dec 07, 2003 11:20 pm |
|
|
Hi guys. I have been at this for hours trying to figure out whats going
on with pin rb1's output.
I checked all the normal stuff and my hardware is ok.
AnyWay here is the scoop. When my switch "toggle" is not pushed
both leds light up fine. When I press the switch d3led turns off but
the other led does not turn all the way off. This is the one connected
to pin rb1.
Measuring voltage at the actual pin it goes from 5vdc to 3.9 when the
switch is pushed...
I re wrote the code to test the pins such as turn on both leds delay
for 10 seconds then turn them off. This worked ok.
Im thinking something is getting confused in the while loop but cant
seem to figure it out...
Thanks for any help. Josh.
code
#include <16F873.h>
#byte porta = 0X05
#byte portb = 0X06
#byte portc = 0X07
#bit TOGGLE = PORTB.0
#BIT Led = PORTB.1
#bit D3LED = PORTC.2
#define on 1 // Logic Value = 1
#define off 0 // Logic Value = 0
#define pushed 0 // Logic Value = 0
// #define True 1 Defined in 16f873.h file
// #define False 0 Defined in 16f873.h file
//////////////////////////////////////////////////////////////////////////////////////
Void main(void){ // The Actual Program.
set_tris_b(0b00000001);
set_tris_c(0b00000000);
while (true) // While Loop Shall Run Forever.
{
if (toggle==pushed){ // Test to see if toggle goes low
d3led=off; // If Toggle was pushed turn off led
Led=off;
}
else d3led=on; // OtherWise Keep Led Turned On.
Led=on;
}} // end of while loop. End of Main
//////////////////////////////////////////////////////////////////////////////
CCS PCM C Compiler, Version 3.112, 12906
Filename: D:\PIC_PROCESSOR\IO.C.LST
ROM used: 36 (1%)
Largest free fragment is 2048
RAM used: 6 (3%) at main() level
6 (3%) worst case
Stack: 0 locations
*
0000: MOVLW 00
0001: MOVWF 0A
0002: GOTO 004
0003: NOP
.................... #include <16F873.h>
.................... //////// Standard Header file for the PIC16F873 device ////////////////
.................... #device PIC16F873
.................... #list
....................
....................
.................... #byte porta = 0x05
.................... #byte portb = 0x06
.................... #byte portc = 0x07
....................
.................... #define TOGGLE pin_b1 // Switch "Change here" fails to work
.................... #define D3LED pin_c1 // LED
....................
.................... Void main(void){
0004: CLRF 04
0005: MOVLW 1F
0006: ANDWF 03,F
0007: MOVLW 07
0008: BSF 03.5
0009: MOVWF 1F
000A: MOVLW FF
000B: BCF 03.5
000C: MOVWF 25
....................
.................... set_tris_b(0b00000001);
000D: MOVLW 01
000E: BSF 03.5
000F: MOVWF 06
.................... set_tris_c(0b00000000);
0010: MOVLW 00
0011: MOVWF 07
0012: BCF 03.5
0013: MOVWF 25
....................
.................... while (true)
.................... {
.................... output_low(d3led);
0014: BCF 25.1
0015: MOVF 25,W
0016: BSF 03.5
0017: MOVWF 07
0018: BCF 03.5
0019: BCF 07.1
....................
.................... if (input(toggle==0))
001A: BTFSS 00.0
001B: GOTO 022
....................
.................... {
.................... OutPut_High(d3led); //LED GOES HIGH:
001C: BCF 25.1
001D: MOVF 25,W
001E: BSF 03.5
001F: MOVWF 07
0020: BCF 03.5
0021: BSF 07.1
.................... }
.................... }}
0022: GOTO 014
....................
0023: SLEEP |
|
|
Kenny
Joined: 07 Sep 2003 Posts: 173 Location: Australia
|
Re: Pin RB1 on 16f873 fails to go completely low. |
Posted: Mon Dec 08, 2003 12:15 am |
|
|
Josh Davis wrote: |
Void main(void){ // The Actual Program.
set_tris_b(0b00000001);
set_tris_c(0b00000000);
while (true) // While Loop Shall Run Forever.
{
if (toggle==pushed){ // Test to see if toggle goes low
d3led=off; // If Toggle was pushed turn off led
Led=off;
}
else d3led=on; // OtherWise Keep Led Turned On.
Led=on;
}} // end of while loop. End of Main
|
The led is being turned on and off at high speed giving the appearance of being half on (or half off :-).
Need to put braces around the two statements
else {
d3led=on; // OtherWise Keep Led Turned On.
Led=on;
}
Also, you can use the CCS function output_bit(), saves handling tris and defining ports. |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Mon Dec 08, 2003 6:31 am |
|
|
Hi Josh
In your previous post http://www.ccsinfo.com/forum/viewtopic.php?t=17747
you asked the same stuff that where answered with a full and excelent explanation by Ttelmah.
I seems that you don�t take care of his answer and his time.
Humberto |
|
|
Guest
|
|
Posted: Mon Dec 08, 2003 6:44 pm |
|
|
Hello Kenny, The C syntax is driving me crazyyyyyyyy
Anyway your solution with the braces worked for the else statement.
Thanks for the help on this. I do recall in a earlier version of the compiler
the same code would have worked ok without the braces, not too sure
which one it was but I shall try find out. Im still thinking this is a bug
with the compiler reason here is the led on port c goes out and if I hook
up led's to the other pins on port b they will also go out
While the braces do indeed make the fix, without them a user in a similar
situation would more than likely pull there hair out on this. There was some
errata on the 16f873 part from microchip. Perhaps the compiler is not taking
this into account.
I dont know asm well enough to desipher exactly whats going on so other
than your help im at a loss. |
|
|
Guest
|
|
Posted: Mon Dec 08, 2003 6:56 pm |
|
|
Hi Humberto
I did read Ttelma previous post. If you look at my current code question
you can see that it uses some of the points he outlined for me.
If perhaps the CCS help file was written by a person that did not assume
we all have years of experience with C, my questions might not appear to be so trivial..
For example look at the ccs help for a for loop. Here is what it says
Statement: for (expr1;expr2;expr3) stmt;
Example:
for (i=1;i<=10;++i)
printf("%u\r\n",i);
That in my humble opinion is a very poor example. No mention about the
int i that needs to be used, not even a couple of lines of supporting
code to explain whats going on.
Whithout stuff like Main, device=, int's etc...... That like the other
CCS Examples are about as useless as a sack of rocks.
You have to have a good working idea with C otherwise your dead in
the water. CCS Needs to look closely at ther hlp file as it sucks. Thankfully
they have this fourm, without it their help desk would be over run with calls.
Thanks... Josh. |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Mon Dec 08, 2003 8:39 pm |
|
|
Quote: |
Anyway your solution with the braces worked for the else statement.
Thanks for the help on this. I do recall in a earlier version of the compiler
the same code would have worked ok without the braces, not too sure
which one it was but I shall try find out. Im still thinking this is a bug
with the compiler reason here is the led on port c goes out and if I hook
up led's to the other pins on port b they will also go out
While the braces do indeed make the fix, without them a user in a similar
situation would more than likely pull there hair out on this. There was some
errata on the 16f873 part from microchip. Perhaps the compiler is not taking
this into account.
|
The braces are not "a fix". That is how you are supposed to code it! Without them
will be executed everytime through the main loop! This is the way your code would be executed:
Code: |
void main(void)
{
set_tris_b(0b00000001);
set_tris_c(0b00000000);
while (true) // While Loop Shall Run Forever.
{
if (toggle==pushed) // Test to see if toggle goes low
{
d3led=off; // If Toggle was pushed turn off led
Led=off;
}
else
{
d3led=on; // OtherWise Keep Led Turned On.
}
Led=on;
}
}
|
Notice that Led = on is sitting there all by its lonesome. If you want more than 1 statement to execute in an if/else you need braces around them.
And now the right way:
will be executed everytime through the main loop! This is the way your code would be executed:
Code: |
void main(void)
{
set_tris_b(0b00000001);
set_tris_c(0b00000000);
while (true) // While Loop Shall Run Forever.
{
if (toggle==pushed) // Test to see if toggle goes low
{
d3led=off; // If Toggle was pushed turn off led
Led=off;
}
else
{
d3led=on; // OtherWise Keep Led Turned On.
Led=on;
}
}
}
|
|
|
|
Guest
|
|
Posted: Mon Dec 08, 2003 10:17 pm |
|
|
Ok Mark, I got it Thanks for taking the time to point me in the right direction.
In the actual code are the braces treated like a nop or pause, or is it just
a method of isolating the code segment that you want to execute based
on the entry point of the last statement.
The c syntax seems to screw me up alot.. Guess I need to practice more.
Thanks again . Josh. |
|
|
prof Guest
|
About RB1 Problem, |
Posted: Tue Dec 09, 2003 3:56 am |
|
|
Please try to use "#fuses NOLVP". See you, |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Tue Dec 09, 2003 7:01 am |
|
|
The braces define a group of instructions to be executed if the conditional statement is met. They do not provide and sort of pause.
Mark |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Tue Dec 09, 2003 9:15 am |
|
|
Josh Davis wrote:
Quote: |
Hi Humberto
I did read Ttelma previous post. If you look at my current code question
you can see that it uses some of the points he outlined for me.
|
Hi Josh, my comments was regarding your code:
Code: |
while (true) // While Loop Shall Run Forever.
{
if (toggle==pushed) // <<<<<<<< !!!! Here
{ // Test to see if toggle goes low
d3led=off; // If Toggle was pushed turn off led
Led=off;
}
else
{
d3led=on; // OtherWise Keep Led Turned On.
Led=on;
}
}
} // end of while loop. End of Main
|
that generate this code:
Code: | ....................
.................... while (true) // While Loop Shall Run Forever.
.................... {
.................... if (toggle==pushed){ // Test to see if toggle goes low
0014: MOVLW 00
0015: BTFSC PORTB.0
0016: MOVLW 01
0017: XORLW 00
0018: BTFSS STATUS.2
0019: GOTO 01D
....................
.................... d3led=off; // If Toggle was pushed turn off led
001A: BCF PORTC.2
|
Using this code:
Code: |
#use fast_io(A)
#use fast_io(B)
#define TOGGLE PIN_B0
//#bit TOGGLE = PORTB.0
#BIT Led = PORTB.1
#bit D3LED = PORTC.2
Void main(void)
{ // The Actual Program.
set_tris_b(0b00000001);
set_tris_c(0b00000000);
while (true) // While Loop Shall Run Forever.
{
if(input(!TOGGLE)) // <<<<<<<<<
{ // Test to see if toggle goes low
d3led = off; // If Toggle was pushed turn off led
Led = off;
}
else
{
d3led=on; // OtherWise Keep Led Turned On.
Led=on;
}
} // end of while loop. End of Main
}
|
you get what you want:
Code: |
................. while (true) // While Loop Shall Run Forever.
.................... {
.................... if(input(!TOGGLE))
.................... { // Test to see if toggle goes low
0014: BTFSC PORTB.0
0015: GOTO 019
.................... d3led = off; // If Toggle was pushed turn off led
0016: BCF PORTC.2
.................... Led = off;
0017: BCF PORTB.1
.................... }
.................... else
.................... {
0018: GOTO 01B
.................... d3led=on; // OtherWise Keep Led Turned On.
0019: BSF PORTC.2
.................... Led=on;
001A: BSF PORTB.1
.................... }
.................... } // end of while loop. End of Main
001B: GOTO 014
.................... }
....................
|
Hope you undestand what I mean.
Regards,
Humberto |
|
|
|