Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
LinkBack Thread Tools Display Modes
Old 10th February 2005, 10:51 PM   (permalink)
Default basic led blinking with micro controller

I know, this is basic but I am having trouble getting this to work. I want to do it in C and am using the PIC C compiler. I also am using a PIC18F452 micro controller.

In the I/O tab in setup i selected ports D0-D7 and C0-C7 as outputs.
everything else I left default. here is the code i made:
Code:
#include "H:\windowsdata\Desktop\Embedded\blink\blink.h"


void main()
{

   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_psp(PSP_DISABLED);
   setup_spi(FALSE);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
   set_tris_c(0);
   set_tris_d(0);
   
   while(1) {

   OUTPUT_HIGH(PIN_D0);
   delay_ms(100);
   OUTPUT_HIGH(PIN_D1);
   delay_ms(100);
   OUTPUT_HIGH(PIN_D2);
   delay_ms(100);
   OUTPUT_HIGH(PIN_D3);
   delay_ms(100);
   OUTPUT_HIGH(PIN_D4);
   delay_ms(100);
   OUTPUT_HIGH(PIN_D5);
   delay_ms(100);
   OUTPUT_HIGH(PIN_D6);
   delay_ms(100);
   OUTPUT_HIGH(PIN_D7);
   delay_ms(100);
   OUTPUT_HIGH(PIN_C0);
   delay_ms(100);
   OUTPUT_HIGH(PIN_C1);
   delay_ms(100);
   
   }
}
and here is the header:
Code:
#include <18F452.h>
#device adc=8
#use delay(clock=20000000)
#fuses NOWDT,WDT128,RC_IO, NOPROTECT, NOOSCSEN, BROWNOUT, BORV20, NOPUT, STVREN, NODEBUG, NOLVP, NOWRT, NOWRTD, NOWRTB, NOWRTC, NOCPD, NOCPB, NOEBTR, NOEBTRB
Yet I can not get a led to blink on any of those ports. What am I doing wrong? thanks
Mejinx is offline  
Old 10th February 2005, 11:07 PM   (permalink)
Default

You must set TRIS registers in order to use the ports as outputs. Use 'set_tris_c(0);' and 'set_tris_d(0);' before your while loop.

Good luck!
DJ_ReL@X! is offline  
Old 10th February 2005, 11:30 PM   (permalink)
Default

thanks for the reply, I updated the code and am still unable to get any leds blinking off of those ports. any other suggestions? thanks
Mejinx is offline  
Old 11th February 2005, 12:12 AM   (permalink)
Default

I have a simple blink program for the 452 that I use as a 'does it work?' routine that I'll post as soon as I get to my main machine. I'm on my PDA at the moment. Until then, did you make sure you decoupled the power to the chip? I mean, a 10uf cap between Vcc and gnd near the chip? just a thought.
__________________
Hop
I think, therefore I crash
Hopperito is offline  
Old 11th February 2005, 12:19 AM   (permalink)
Default

yup, have the cap...circuit works fine, that I know for a fact. My friend used this exact setup for his labs and we checked it already with some of his code. Problem is I am using C and he doesnt really ever use it. So we are both at a loss.
Mejinx is offline  
Old 11th February 2005, 12:25 AM   (permalink)
Default

(deleted)
panic mode is offline  
Old 11th February 2005, 02:06 AM   (permalink)
Default

Here is my code to blink 5 LED's (like Knightrider) with 16F877A on the MSB's of PORTB:
Code:
/////////////////////////////////////////////////////////////////////////
////                           LED1C.C                               ////
////                                                                 ////
////  Enciende 5 LED's sucesivamente                                 ////
/////////////////////////////////////////////////////////////////////////
////                                                                 ////
/////////////////////////////////////////////////////////////////////////

#include <16F877A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,NOPUT,NOBROWNOUT
#use delay(clock=8000000)
#use fast_io(B)

void delay_meds(int n)
{
  for(n;n!=0;n--)
  delay_ms(50);

}

main()
{
  set_tris_b(0x00);
  while(TRUE)
    {
    output_b(0x80);
    delay_meds(1);
    output_b(0x40);
    delay_meds(1);
    output_b(0x20);
    delay_meds(1);
    output_b(0x10);
    delay_meds(1);
    output_b(0x08);
    delay_meds(1);
    output_b(0x10);
    delay_meds(1);
    output_b(0x20);
    delay_meds(1);
    output_b(0x40);
    delay_meds(1);
    }
}
I was also testing sub-functions. You can replace my delay_meds() function with delay_ms() and delete void delay_meds() {...}.
DJ_ReL@X! is offline  
Reply

Bookmarks

Thread Tools
Display Modes





All times are GMT. The time now is 07:51 AM.


Electronic Circuits  |  Learning Electronics
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.

eXTReMe Tracker