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.

Light up led source code - what went wrong ?

Status
Not open for further replies.

evaang2003

Newbie level 6
Joined
May 28, 2009
Messages
14
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Activity points
1,436
light up led source code

hi, i currently play with led using pic16f877a.
i connect one led to port b bit7.
the result i want to get is just to lit the led.
however, it does not response.
here is the source code i write using ccs picc compiler.

#include <16f877a.h>

void main()
{
output_high(PIN_B7);
}

can anybody tell me what went wrong?
 

light up led source code

you must make the B7 output..

TRISB7=0;

after

PB7=1;
 

light up led source code

Yes you have to clear the the TRISB7 bit to make this pin output make sure about CONFIG word if DEBUG is enabled this pin may not function.
 

Re: light up led source code

I would suggest:
Code:
#include <16f877a.h> 

void main() 
{ 
output_high(PIN_B7);
while(1); 
}

In your code, after the the LED is on, the microcontroler reaches the end of the program and resests. So the result will be a blinking of the LED with a very small duty factor and maybe that's why you can't see any light.

I've never used CCS compiler but maybe you need to set pin RB7 as output separately before putting it high.
 

Re: light up led source code

well, i modify the code to:

#include<16f877a.h>

#BYTE PORTB=0x06
#BIT PORTB7=PORTB.7

void main(void)
{
set_tris_b(0x00);
output_high(PIN_B7);
while(1);
}

sadly, it din work:(
 

light up led source code

can you tell me which compiler you r using?

maybe your problem is hardware..

Try run in simulation. it works?
 

Re: light up led source code

I can't see in your code if you set the configuration bits of the controller, especially the oscillator configuration...
 

Re: light up led source code

i just want to test the i/o function of pic16f877a.
i experiment it with one led, 4MHz oscilator but it din work.
i m using ccs compiler.

i then include these into source code but it din work.
#use dalay(clock=4000000)// 4MHz crytal is used.

it din work.
why???
 

Re: light up led source code

What exactly are you expecting the LED to do?
The way you created it, the moment the chip gets power it'll turn the LED on and that's it. If you want it to blink, you'll need to turn it on, wait and then turn it off. You do this by using a delay function, usually called DelayMs(X) where X is the number of milliseconds you want to wait.
 

Re: light up led source code

i now know the fatal error that occur.
my compiler is damaged that it generate rubbish hex code.

thanks for those try to help me.
i will work it on after i get the new compiler.
 

I had the exact same problem trying both the ASM assmembler and also the CCS Demo compiler. It can be very frustrating when not anything seems to work:
One thing I found was my source code for c was glitched out. I went and got a source code out from the compiler examples and it would build then low and behold, I deleted everything out and put in my code and it also did compile. So here are two source files to accomplish what you want to light an LED -- I used GP1 on the 508 because I could not get GP2 to ever take as an output port -- all the others did however; Anyway here are two souce files for both ASM and C:

ASM==>
list p=12F508 ; list directive to define processor
#include <p12F508.inc> ; processor specific variable definitions

; Internal RC calibration value is placed at location 0x1FF by Microchip
; as a movlw k, where the k is a literal value.

ORG 0x1FF ; coding begins here -- this is required to build file!!!
movwf OSCCAL ; set internal oscillator with factory value

__CONFIG _MCLRE_OFF &_CP_OFF & _WDT_OFF & _IntRC_OSC
;_WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_OFF)

; '__CONFIG' directive is used to embed configuration word within .asm file.
; The lables following the directive are located in the respective .inc file.
; See respective data sheet for additional information on configuration word.

;***** VARIABLE DEFINITIONS
;temp EQU 0x07 ;example variable definition

;**********************************************************************
ORG 0 ; processor reset vector this is required to build file!!!

; Internal RC calibration value is placed at location 0x1FF by Microchip
; as a movlw k, where the k is a literal value.


start
movlw b'111101' ; confiqure GP1 as an output
tris GPIO ; send w to GPIO PORT
movlw b'000010' ; set GP1 as output of '1'
movwf GPIO ; send '1' to GP1
goto $ ; wait here

end ; must enter release set by going to PROGRAMMER -ENTER 'release from reset'
; before the PIC 508 will run-- to turn on PORT GPIO 1!!!

C CODE =>

#if defined(__PCB__)
#include <12F508.h>
#fuses INTRC,NOWDT,NOPROTECT, NOMCLR
#use delay(clock=4000000)
#define GP0 PIN_B0
#define GP1 PIN_B1
#define GP2 PIN_B2
#define GP3 PIN_B3
#define GP4 PIN_B4
#define GP5 PIN_B5

main(void)
{
output_high(GP1);
while (TRUE){
for(;;);
}

Added after 11 minutes:

Those two source codes will work with the PicKit2 both ASM and CCS Demo -- sorry but that last bit of C code got glitched it should read on the bottom

for (;;):

}

}

As a note when I ran the ASM code -- I had to find out that you need to go to PROGRAMMER again and hit the 'Release Reset' before it will download the program to the 508 and actually turn the light on. This took me a couple of days to find this out because I could get the 16F690 to run -- evidently you do not need to do this with the 690 but I could not get the 508 to actually download to the chip until I hit the "Release Reset" option under PROGRAMMER. Good luck with things!
 

How you connect the led with the microcontroller,
5v-->LED-->microcontroller pin
or
microcontroller-->LED-->GND
What are your fuses settings?.Post it
 

This mesage system is messed it keeps putting smily faces in for some of the code.

That last While(TRUE) should be closed with

for (semicolin semicolin); only the semicolins are actual -- Evidently I have to write them out here or I will get stuck with a smiley FACE!!! -- Awesome!! Anyway you do need to close the While () loop with the for ( semicolin semicolin); with actual semicolin = ; -- just trying to get arround the creative smiley face thing going on with this message!!!

Added after 8 minutes:

If you are using the PicKit2 Demo board?? Which is what I have -- then take a soddering iron and put a lead on the RA1 position on the board -- then you can either touch that lead to any of the available LEDs on the Board -- or you can plug it into the Board Output clip for any of the corresponding LEDS.

A Third Alternative is to actually get another LED and a 220 Ohm resistor and sodder the into the extra space on the board and put one lead to ground (VSS) sodder slot and the other lead to your GP1 pin. I like having the lead free to plug into any of the LED positions -- you can experment around to find the pin-out location on the external pin sockets. or just touch this lead directly to the LED pin for each respective light.

You can experiment around with this code to turn various GPIOs on the 508 to output -- I found that any of them would work, except surprisingly the GP2 pin, it would never program in either C or ASM though according to the chip tech report it should??

Added after 18 minutes:

Here is that ASM source with the comments deleted out so you can see what is really essential to make it build and run: (left is the last comment to go to PROGRAMMER and hit 'Release Reset' -- if you do not do this it will not download the hex file to the 508 chip:

ASM File (with comments deleted)=>

list p=12F508
#include <p12F508.inc>
ORG 0x1FF
movwf OSCCAL
__CONFIG _MCLRE_OFF &_CP_OFF & _WDT_OFF & _IntRC_OSC
ORG 0
start
movlw b'111101'
tris GPIO
movlw b'000010'
movwf GPIO
goto $

end ; must enter "release reset "by going
;to PROGRAMMER then ENTERING 'release from reset' option
; before the PIC 508 will run-- to turn on LED on PORT GPIO 1!!!
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top