Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] PIC mcu - interfacing button not working

Status
Not open for further replies.

Deexith Hasan

Advanced Member level 4
Joined
Oct 24, 2014
Messages
111
Helped
4
Reputation
8
Reaction score
3
Trophy points
18
Activity points
740

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <htc.h>
 
void main()
{   
TRISB0=1;
TRISD0=0;
RD0=0;
  
while(1)
  {  
    if(RB0)
     RD0=1; 
    else
    RD0=0;
  }
}

 
Last edited by a moderator:

hello

MCU type ? 16F , 18F other
Config bits ?
internal or quartz oscillator running ?
Inhibit analog config by default if necessary
 

Change RD0 to LATD0. Use debounce delay for switch.
 

The default system clock source is an external oscillator (FOSC = 0x7). Looking at your circuit, there is no external oscillator so you need to set the FOSC to at least 0x8 (0x9 is also an option). That way you will have a system clock that lets the device run.
(By the way, if you used a debugger, this would have been obvious when it would tell you that it cannot connect to the device - without a system clock you can program it but nothing else.)
There are other configuration bits you need to set - for a start you should turn off the brownout (I suggest BOREN = 0), turn off the watchdog (WDTEN = 0). You need to look at section 23.1 in the data sheet to see the default settings of the various config options (these are the ones that configure the processor when it powers on - some can be altered later on in your code) and also look at the compiler user guide as to how these can be set the way you need them.
In your case you also need to make sure that RB0 is set to analog mode. Most Microchip devices power up with any pin that has analog capability set to analog mode and you need to look for the correct register to make it operate as a digital pin. (Generally, when a pin can be used for multiple functions, the order they are listed is important. For this device it would seem that the function names are listed from lowest to highest priority (note that the PIC24, dsPIC and many other families have this the other way around) so that "RB0/INT0/FLT0/AN12" means the analog function will take precedence and therefore must be turned off to let the pin operate for any other function. (I see that the naming order in the data sheet is NOT the same as on your circuit - use the data sheet always.)
To set the pin to be digital, the data sheet shows there are 2 ways: with the PBADEN config bits (setting this to 0 will make PORTB analog bits power up in digital mode); alternatively use the ADCON1 register to set the PCFG bits to 0x3 or higher (you are using RB0 which has AN12 listed - therefore you must made at least AN12 digital).
It was already mentioned that you should set the value of any output pin using the LAT - the general rule is to read from the PORT, write to the LAT.
While it is not shown on your circuit, I assume that you have ALL of the power supply pins connected and properly decoupled. The data sheets for some devices (not this one apparently) also show the absolute minimum power and other connections required but be on the safe side and properly connect every supply pin.
It was mentioned above that you should debounce the input pin (RB0 in your case). While your program will work without this, it will get you into a bad habit later on that will cause you many wasted hours. Mechanical switches do not open and close cleanly - they typically bounce and send multiple pulses for anything up to several mSec. If you try to respond to each of these transitions (e.g. by incrementing a counter) then your program will think it is getting multiple switches for each push or release. There are many hardware and software examples on the internet as to how to debounce an input pin and I would strongly recommend that you look into these while you are still in the early stages of getting a very simple programs working. Once you see how to handle this, you can avoid the problems when you start on more complex programs.
There is a steep learning curve when using Microchip devices but the above should get you quite a long way. There are a number of code examples on the net and they form a useful way to get going by letting you get a program running quickly and also seeing what needs to go into even a simple program such as yours.
Susan
 
can u send me the code for making any pin as input in PIC18F mcu
 

Tey attached project. There are two versions. One without external interrupts and one with external interrupt.
 

Attachments

  • LED SW.rar
    59.5 KB · Views: 103

got error while running the code on HI-TECH c compiler....
 

It is mikroC PRO PIC compiler code. Download and install mikroC PRO PIC compiler from mikroElektronika. The demo version will compile the code.

- - - Updated - - -

Edit:

You have to use attached circuit. Tomorrow I will give you interrupt based Hi-Tech C code.

Replace Delay_ms(100); by Hi-Tech C delay ms function. You have to set the config words in code or IDE. Especially FOSC type and WDT should be configured.

choose XT type for FOSC and OFF for WDT.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <htc.h>
 
#define _XTAL_FREQ 4000000
 
#define INPUT 1
#define OUTPUT 0
 
#define ON 1
#define OFF 0
 
#define LED LATbits.LATD0
#define LED_Direction TRISDbits.TRISD0
 
#define SW PORTBbits.RB0
#define SW_Direction TRISBbits.TRISB0
 
void Debounce() {
    Delay_ms(100);
}
 
void main() {
 
     ADCON1 = 0x0F;
     CMCON = 0x07;
     CVRCON = 0x00;
     
     TRISA = 0xFF;
     TRISB = 0x01;
     TRISC = 0x00;
     TRISD = 0x00;
     TRISE = 0x08;
     
     PORTA = 0x00;
     PORTB = 0x00;
     PORTC = 0x00;
     PORTD = 0x00;
     PORTE = 0x08;
     
     LATA = 0x00;
     LATB = 0x00;
     LATC = 0x00;
     LATD = 0x00;
     LATE = 0x00;
     
     SW_Direction = INPUT;
     LED_Direction = OUTPUT;
     
     while(1) {
     
           if(SW) {
               Debounce();
               while(SW);
               
               LED = ~LED;
           }
     }
}

 

Attachments

  • ckt.pdf
    56.4 KB · Views: 100
Last edited:
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top