Microcontroller › PIC › Novice at Microcontrollers
- This topic has 10 replies, 2 voices, and was last updated 11 years, 11 months ago by AJISH ALFRED.
-
AuthorPosts
-
November 19, 2012 at 4:43 am #1998RenParticipant
Hi all im a newbie at writing code in C for programing microcontrollers specfically the PIC 18F 4550.
Im trying to control 3 electrical devices, a LED, and two small DC motors.
Any help will be grately appreciated.
November 19, 2012 at 4:35 pm #8766AJISH ALFREDParticipantHi Ren,
Connect the LED directly to any of the port pin and write a simple code to glow the LED. You can connect the motor using ULN2004 driver IC. For driving the motor the logic is same as glowing the LED
November 20, 2012 at 12:03 am #8768RenParticipantI’m using momentary switches, specifically NO (Normally Open Switches) to send my signals of high to the input, to activate them. The problem I’m encountering is, how to send one signal, to turn the motor on, and keep it on, until another signal is sent to turn it off. My intension is to set up a device to feed the input ports BCD (Binary Coded Digit) to switch the devices on and off.
e.g, say 0001 = motor on
0010 = motor off
I have the LD293DNE chip, availble I think it should work for powering the motor.
Thanking you in advance.
November 20, 2012 at 4:12 pm #8773AJISH ALFREDParticipantHi Ren,
Thanks for sharing that much information. We will surely help you with the project.
You can connect the L293D directly to any of the digital pin of the controller. Go through the datasheet and you can find the sample circuit for connecting the motor. Pull up the pins which you intent to connect the switch in such a way that when you press the key the digital pin should receive logic 0 otherwise logic 1. Thats all about the hardware.
Now coming to the programming part wait till the digital pin goes low (happens when you press the key) and once low activate the pin in which the l293D is connected, add a delay. Now wait till the pin goes low again and if it does deactivate the L293D pin.
Do all these in an infinite loop.
Expecting your doubts!
November 21, 2012 at 2:23 pm #8775RenParticipantI’m using MPLAB IDE v8.88 and c18 compilier and I got an LED to light reading input by using a sample program on this site but, I have a major problem, before I can even sent 5V on portb to send it high, the LED on porte lights, matter of fact any wire, slight touch on any of the portb input pins causes it the LED on pote to light or stay on for a while, worst case it just comes on and stays on. The next problem is that, the porte output is about 2.5V max. From the looks of things I’m in deep trouble, here is the code.
//config.
#include <p18F4550.h>
#pragma config FOSC = INTOSC_EC
#pragma config PWRT = OFF
#pragma config BOR = OFF
#pragma config MCLRE = ON
#pragma config PBADEN = OFF
#pragma config ICPRT = OFF
#pragma config LVP = OFF
#pragma config WDT = OFF,DEBUG=OFFvoid main()
{
ADCON1=0x0F; // Set all pins as digital I/O
CMCON=0x07; // Set all comparators as digital I/O
TRISEbits.RE0=0; // Configure pin RE0 as output
TRISBbits.RB0=1; // Configure pin RB0 as input
while(1)
{
if(PORTBbits.RB0=1) // If input is high
LATEbits.LATE0=1; // Turn the LED On
else
LATEbits.LATE0=0; // Turn the LED Off
}
}November 22, 2012 at 5:29 pm #8780AJISH ALFREDParticipantsorry for the late response.
have you checked the voltage after removing the led? please post your led glowing circuit.
November 22, 2012 at 5:35 pm #8782AJISH ALFREDParticipantdo it in this way.
don’t write anything inside the while (1) loop
befor while loop write a code for turn the led on and after sometimes it go off.
void main()
{
ADCON1=0x0F; // Set all pins as digital I/O
CMCON=0x07; // Set all comparators as digital I/O
TRISEbits.RE0=0; // Configure pin RE0 as output
TRISBbits.RB0=1; // Configure pin RB0 as inputLATEbits.LATE0=1; // Turn the LED On
delay_ms (3000); //3 seconds delay here
LATEbits.LATE0=0; // Turn the LED Offwhile(1)
{
;
}
}If your port settings and the circuit are alright, the led will on first then after 3 seconds it will go off. Please try and update
November 24, 2012 at 5:36 am #8786RenParticipantWhy when I set any port as input and I write a condition for the output, I get the output regaurdless of thye condition, for the pic16f690. i.e
my code:
#include <htc.h>
#pragma config FOSC=INTRCIO
#pragma config WDTE=OFF
#pragma config PWRTE=OFF
#pragma config MCLRE=ON
#pragma config CP=OFF
#pragma config CPD=OFF
#pragma config BOREN=OFF
#pragma config IESO=OFF
#pragma config FCMEN=OFFvoid main(void)
{
ANSEL=0x00;
ANSELH=0x00;TRISBbits.TRISB4=1;
// TRISAbits.TRISA1=1;
// TRISAbits.TRISA2=1;
// TRISBbits.TRISB5=1;
TRISC=0;while (1){
if (PORTBbits.RB4=1)
(PORTC=0xff);
}
}I get Port c lighting up and no input triggered it.
November 24, 2012 at 5:57 pm #8788AJISH ALFREDParticipantHi Ren,
while (1)
{
if (PORTBbits.RB4=1) //THIS IS NOT A CONDITIO, BUT AN ASSIGNMENT STATEMENT
(PORTC=0xff);
}
The correct coding is,
while (1)
{
if (PORTBbits.RB4==1) //SEE THE LOGICAL OPERATOR “==”
(PORTC=0xff);
}
You should practice more C coding and I hope you won’t do these kind of silly mistakes again
November 24, 2012 at 8:50 pm #8792RenParticipantWell I get what you are saying but it still doesnt work. Hmm does the Vss pin need to be properly grounded?
November 25, 2012 at 5:17 pm #8793AJISH ALFREDParticipantPlease post the complete connection diagram.
-
AuthorPosts
- You must be logged in to reply to this topic.