|
|
View previous topic :: View next topic |
Author |
Message |
AA
Joined: 13 May 2005 Posts: 3
|
Need some input output help, AND questions about LCD |
Posted: Sun May 15, 2005 7:48 am |
|
|
Well, im quite new to pic's and to this compilator..
Anyway, im using 18F452 and im trying to make it react to an input on Pin_a1 by flashing a LED on B2, and while it doesnt have the input, i would like it to flash B1. But now when i start it, B2 flashes for hmm maybe a minute then B1 starts to flash, it reacts on the input but B2 doesnt stop to flash when i remove it (the input on A1), after a while it stops and B1 begins to flash again.
MY CODE
-----------------------------
#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=12000000)
#define sensor_pin PIN_A0
#define LED_PIN1 PIN_B1
#define LED_PIN2 PIN_B2
void init()
{
SETUP_ADC_PORTS(NO_ANALOGS);
set_tris_b(0x00); // RB = OUTPUT
output_b(0x00);
set_tris_a(0xFF); //RA = INPUT
}
void hitta()
{
if(input(sensor_pin) == 0) {
output_high(LED_PIN1);
delay_ms(100);
output_low(LED_PIN1);
delay_ms(100);
}
if(input(sensor_pin) == 1){
output_high(LED_PIN2);
delay_ms(100);
output_low(LED_PIN2);
delay_ms(100);
}
}
void main() //Main program start here
{
init();
while(1){
hitta();
}
}
---------
Last edited by AA on Sun May 15, 2005 1:19 pm; edited 1 time in total |
|
|
PhilWinder
Joined: 16 Apr 2005 Posts: 23
|
|
Posted: Sun May 15, 2005 10:12 am |
|
|
code seems ok, whenever i get anything wrong, its usually the hardware, so give it a good check over.
firstly check that you have pullups on the inputs, sounds like its floating.
then check osc value, cap values and mclr, but it wouldnt work at all if that was disconnected.
other than that, you could try something a bit simpler, like first getting the led to flash, then get it to turn on when you hit a button, then put the two together to make this.
phil |
|
|
Guest
|
|
Posted: Sun May 15, 2005 1:17 pm |
|
|
I made it work with this code instead, using interuppts:
MY CODE
------------------------
#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=12000000)
#int_ext //Set external interrupt on pin RB0
void ISR()
{
output_low(pin_b4); // when RB0 is high turn off LED
delay_ms(1000);
} // End Interrupt routineb3
#int_ext2
void ISR2()
{
output_high(pin_b4); //when RB1 is high turn on led
delay_ms(1000);
}
void init()
{
output_b(0x00);
set_tris_b(0x03); // Set RB0, RB1 to input, RB2-7 output
ext_int_edge(1,L_TO_H); // init interrupt triggering for button press
ext_int_edge(2,H_TO_L);
enable_interrupts(INT_EXT);
enable_interrupts(INT_EXT2);
enable_interrupts(GLOBAL);
}
void hitta()
{
while (1) // keep LED flashing
{
output_high(PIN_b4);
delay_ms(100);
output_low(PIN_b4);
delay_ms(100);
}
}
void main() //Main program start here
{
init();
hitta();
}
------------------
Now im wondering if i can run a LCD (www.farnell.com/datasheets/52032.pdf) 16x2 along with this code, im not so good at this yet but what i can understand the pic.c wants to use port B to work with the LCD but my port B is busy with handling the interrupts.
Thanks |
|
|
AA
Joined: 13 May 2005 Posts: 3
|
|
Posted: Sun May 15, 2005 1:19 pm |
|
|
oops forgot to log in |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun May 15, 2005 1:57 pm |
|
|
Quote: | Now im wondering if i can run a LCD along with this code.
but my port B is busy with handling the interrupts. |
You need to use Mark's improved version of the CCS LCD driver.
He has expanded the "lcd_pin_map" structure so that the control
signals and the group of 4 data lines can be moved to other ports,
and also split up between different ports. His example fits the
Microchip PicDem2 Plus board, which has the control signals on
port A, and the data signals on Port D. You can change it to
fit your hardware. See the following link:
http://www.ccsinfo.com/forum/viewtopic.php?t=20182 |
|
|
AA
Joined: 13 May 2005 Posts: 3
|
|
Posted: Sun May 15, 2005 2:30 pm |
|
|
Thanks for the reply!
Quote: |
BOOLEAN dummy; // PinA0 is not used
BOOLEAN enable; // PinA1
BOOLEAN rw; // PinA2
BOOLEAN rs; // PinA3
int unusedA : 4; // The rest of portA
int unusedB; // portB is not used
int unusedC; // portC is not used
int data : 4; // lower nibble of portD is used for data lines
int unusedD : 4; // The rest of portD
|
Do i understand it correctly if i say that A1 should be connected to enable on the LCD, A2 to RW and A3 to RS, and then that port A4-A7 is just to connect to the following pins on the LCD then DB0 (etc.. on LCD) connected to the PIC's D0,D1,D2.. ? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun May 15, 2005 2:54 pm |
|
|
No, when a LCD is used in 4-bit mode, you always use the upper 4 bits
on the LCD data bus. That's DB4-DB7.
To see how it's done on the PicDem2 Plus board, go to this page
and down at the bottom, download the User's Guide.
http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1406&dDocName=en010072&part=DM163022
Then in that document, go to Appendix A, Section A.13 Board Layout
and Schematics, and then Figure A-3.
At the top of the page it shows the LCD. They are taking DB4 on the
LCD to pin RD0 on the PIC. Then DB5 to RD1, etc. In other words,
the upper 4 bits on the LCD go to the lower 4 bits on Port D.
If it was me, I would have used the upper 4 bits on Port D, just because
then the bit positions between the PIC and the LCD would have been
one-to-one. It would have been easier. |
|
|
|
|
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
|