I am new to Pic microcontrollers and just started programming them.
Now my problem lies in this code:
unsigned char i;
unsigned char firstRun;
unsigned char secondRun;
unsigned char released;
void main() {
unsigned char i;
unsigned char firstRun;
unsigned char secondRun;
unsigned char released;
i = 0;
firstRun = 1;
secondRun = 0;
released = 0;
TRISIO.B1 = 0x01;
TRISIO.B2 = 0x00;
CMCON0 = 0x07;
ANSEL = 0;
while(1){
if(firstRun == 1){
GPIO.B2 = 1;
}
}
}
Why don't I get any output on this port?
I want the pin to turn on, but it doesn't.
I know that I have hooked it up right on the breadboard.
UPDATE: Im using the 12f683, the MicroC pro compiler and IDE, and im programming using the MPLAB X IPE
It has worked with the line GPIO.B2 = GPIO.B1 inside thw while 1 block GPIO.B1 is input
This dosent work eighter:
char firstRun;
void main() {
TRISIO.B1 = 0x01;
TRISIO.B2 = 0x00;
CMCON0 = 0x07;
ANSEL = 0;
firstRun = 1;
while(1){
GPIO.B2 = 1;
}
}
CONFIG: enter image description here
Assembly:
_main:
;Forste test.c,3 :: void main() {
;Forste test.c,4 :: TRISIO.B1 = 0x01;
BSF TRISIO+0, 1
;Forste test.c,5 :: TRISIO.B2 = 0x00;
BCF TRISIO+0, 2
;Forste test.c,7 :: CMCON0 = 0x07;
MOVLW 7
MOVWF CMCON0+0
;Forste test.c,8 :: ANSEL = 0;
CLRF ANSEL+0
;Forste test.c,10 :: firstRun = 1;
MOVLW 1
MOVWF _firstRun+0
;Forste test.c,11 :: while(1){
L_main0:
;Forste test.c,12 :: GPIO.B2 = 1;
BSF GPIO+0, 2
;Forste test.c,13 :: }
GOTO L_main0
;Forste test.c,14 :: }
L_end_main:
GOTO $+0
-
\$\begingroup\$ what do you expect to get exactly? your code just turn on the pin. \$\endgroup\$Vladimir Cravero– Vladimir Cravero2014年09月13日 22:02:11 +00:00Commented Sep 13, 2014 at 22:02
-
\$\begingroup\$ I want it to turn on the pin, the probmen is that it dosent \$\endgroup\$user52719– user527192014年09月13日 22:07:13 +00:00Commented Sep 13, 2014 at 22:07
-
2\$\begingroup\$ Please update your question with which PIC you're using, which compiler, and which IDE (if any). Also, is this all of your code or just a part of it? \$\endgroup\$Dan Laks– Dan Laks2014年09月13日 22:09:00 +00:00Commented Sep 13, 2014 at 22:09
-
\$\begingroup\$ are you sure that the clock is configured properly? what if you turn your pin on before the while(1) and then turn it off in the if clause? how are you measuring it? \$\endgroup\$Vladimir Cravero– Vladimir Cravero2014年09月13日 22:09:02 +00:00Commented Sep 13, 2014 at 22:09
-
1\$\begingroup\$ I compiled your code with MikroC Pro and programmed it into a 12F683 with PicKit2. It works as expected when driving a LED at 20mA. How much current does your motor need, and how is it connected to the PIC? \$\endgroup\$Bruce Abbott– Bruce Abbott2014年09月14日 00:58:32 +00:00Commented Sep 14, 2014 at 0:58
2 Answers 2
@NickAlexeev seem to have hit upon the problem- this compiler is non-compliant. See this.
If you get rid of the global declarations (outside of main), I bet it works.
I've seen compiler problems with global variables in Hitech C for the baseline PICs as well.
As a rule with PIC's you should use the LAT register to set outputs and the PORT register to read inputs. Try LAT.B2 = 1;
instead of GPIO.B2 = 1;
The syntax may not be exactly LAT, I haven't used a 12F part in probably a decade and I've never used the IPE, but it will be close to that.
-
\$\begingroup\$ The baseline pics don't have the LAT feature- you just have to be careful. In this case, it shouldn't matter. \$\endgroup\$Spehro 'speff' Pefhany– Spehro 'speff' Pefhany2014年09月13日 23:26:12 +00:00Commented Sep 13, 2014 at 23:26