Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

basic led blinking with micro controller

Status
Not open for further replies.

Mejinx

New Member
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
 
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!
 
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
 
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.
 
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.
 
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() {...}.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top