|
|
View previous topic :: View next topic |
Author |
Message |
mesrouk
Joined: 26 Jan 2010 Posts: 3
|
switch case under ccs |
Posted: Thu Aug 25, 2011 1:53 am |
|
|
Hey everybody, I am beginning to learn the c for PIC. I have a small problem I would like to use switch case under ccs c for pic 16F and simulate under Proteus.
The program consist of 2 buttons connecting in pin_b0 and pin_b1 when I use the button who connecting pin b0 there are in pin_c0 signal and for pin_b1 pin_c1 signal.
But when I am compiling there are not error but it's not work in Proteus. There are one led always lighting without touch the button.
Code: |
#include<16F877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#define pin_b0 1
#define pin_b1 2
void main(){
int chose;
port_b_pullups(true);
set_tris_b(0xff);
set_tris_c(0x00);
while((chose>='1'||chose<=2))
switch(chose)
{
case '0': output_high(pin_c0);
delay_ms(300);
output_low(pin_c0);
delay_ms(300);
break;
case '1': output_high(pin_c1);
delay_ms(300);
output_low(pin_c1);
delay_ms(300);
break;
while(true)
{
}
}
} |
Can you help me please |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19537
|
|
Posted: Thu Aug 25, 2011 2:17 am |
|
|
First, learn to use the code buttons.....
Code: |
#include<16F877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#define pin_b0 1
#define pin_b1 2
void main(){
int chose ;
port_b_pullups(true);
set_tris_b(0xff);
set_tris_c(0x00);
while((chose>='1'||chose<=2)) {
switch(chose) {
case '0':
output_high(pin_c0);
delay_ms(300);
output_low(pin_c0);
delay_ms(300);
break;
case '1':
output_high(pin_c1);
delay_ms(300);
output_low(pin_c1);
delay_ms(300);
break;
}
}
while(true) {
}
}
|
Now I have indented the code, and altered the bracketing so it has a little 'sense' to it.
Now some faults:
You declare 'chose' as an integer, but at no point do you put anything _into_ the variable. Typically an uninitialised local integer variable will contain '255' (a static global, will be initialised to 0, or if you have #ZERO_RAM, all variables will initialise to 0).
Then, you mix comparison types. You use '0' with inverted commas (the _text_ character '0'), in some places, and 0 (the number) in others.
You then have defines for 'pin_b0', which are not the values the compiler needs, and will destroy the compiler values of the same name, meaning that a lot of code libraries won't work latter. If you want to name the bits in a port (I'm guessing you really mean to read portb into 'chose', and then test the bits in this?.), use a different name from the default port names.
Then, oscillator. Read the data sheet. Is 'HS' the right oscillator for 4MHz?. Proteus doesn't care about whether the oscillator will run. For a real chip, _you_ have to....
Your main while loop at the end, really needs to be around all the code, or if 'chose' doesn't have the value expected, you will drop into this. The same loop also needs to read the value on port_b, and put this into chose.
Best Wishes |
|
|
mesrouk
Joined: 26 Jan 2010 Posts: 3
|
|
Posted: Mon Sep 05, 2011 1:24 am |
|
|
Always nothing for this program, I tried another program
Code: |
#include <16F877.h>
#fuses HS,NOWDT,NOPROTECT
#use delay(clock=10000000)
#byte port_b=0x03
#byte port_c=0x03
Int8 val ;
Void main() {
val=input_b(); / get the port_b the value 00000001,00000010,00000011,by the variable (val)
while((val>= 0x01& val<=0x03)) / l'interval for switch who corespand in adresses of port_b
while(true){
Switch(val)
{
Case 0x01: / put Rb0=1 by button
output_c(0x01); / l Rc0=1
Break;
Case 0x03: / put Rb0=1,Rb1=1
output_c(0x00); / Rc0=0,Rc1=0
Break;
while(true)
{
output_c(val); / execute Data Recovery in port_C
}
}
}
} |
Can anyone help me and give me the principle use this instruction?
Thank you |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19537
|
|
Posted: Mon Sep 05, 2011 2:02 am |
|
|
Think where your are reading the port. When will this be executed?. You then sit is a loop, and the port value remains what has already been read. What will happen?.
Best Wishes |
|
|
|
|
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
|