Thread: Micro-controller programming basics.

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    5
    Hi - I'm trying to get to grips with programming micro-controllers using c before I go to Uni next year. I'm doing a view tasks on the internet and have written a program to simply turn an led on and off as a biased switch is pressed. I was wondering how I could write a similar program to transfer the state of the lower 5 dil switches (RC0 to RC4) to LEDs (RA0 to RA4) without allowing RC5 to affect anything.




    /*
    A program that causes an LED, connected to RA0,
    to go on and off as the biased switch,
    connected to RC6, is closed and opened.
    All other LEDs (connected to RA1 to RA4) and the buzzer (RA5)
    should be off.
    /*

    #include <p18f2221.h>
    #pragma config OSC=HS /* 0.4us cycle time with 10MHz osc */
    #pragma config WDT=OFF
    #define Switch PORTCbits.RC6
    #define LED PORTAbits.RA0 void main(void){
    ADCON1=0x0f; /* digital IO */
    TRISA=0xc0; /* 6 LEDs as outputs */
    TRISC=0xff; /* PORTC as inputs */
    PORTA=0x00; /* all LEDs off */
    while(1){ /* forever */
    if(Switch) /* is switch open (non zero)*/
    LED=0;

    else
    LED=1; /* turn LED on */
    }
    }

    Any of your help and time would be much appreciated.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    While most of us here are happy to give you a hand with the C language aspects of what you're doing, this is not a microcontroller support site. I'm thinking you will get a lot better information and support by going to the forums or mailing lists connected with your particular development board...

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    5
    Ok thanks - I'll try there

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    If your compiler does not allow defining a variable to access a single bit (it appears that your compiler might) then you will need to use bit masking with the logical operators to alter a single bit in a byte. See this link for an small tutorial.

    Jim

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    43
    depending which micro you have the code would be even simpler, for instance the micro i do all my programing with is the PSoC programmable system on chip and the languages are c or assemble but to just simply turn on an led and turn off, i would just name two ports, one switch and one led

    the code would just simply be
    if(switch_Data_ADDR & switch_MASK)
    { LED _DATA_ADDR ^= LED_MASK
    this would just combine all 8 bits of what ever data registry you had, and the exclusive or would detect the high to low transition of the clock and turn on and off the led every time the switch is pressed

    depending which micro you have the code would be even simpler, for instance the micro i do all my programing with is the PSoC programmable system on chip and the languages are c or assemble but to just simply turn on an led and turn off, i would just name two ports, one switch and one led

    the code would just simply be
    if(switch_Data_ADDR & switch_MASK)
    { LED _DATA_ADDR ^= LED_MASK
    this would just combine all 8 bits of what ever data registry you had, and the exclusive or would detect the high to low transition of the clock and turn on and off the led every time the switch is pressed

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an XBox controller for my PC
    By Mario F. in forum General Discussions
    Replies: 5
    Last Post: 12-14-2010, 09:58 PM
  2. c programming, MC68HC11 micro controller board simulator
    By fortune2k in forum C Programming
    Replies: 10
    Last Post: 03-11-2009, 07:04 AM
  3. What do i need to build a USB E-Brake (Gaming controller)
    By childofbodom16 in forum Game Programming
    Replies: 1
    Last Post: 04-25-2007, 02:16 PM
  4. What do i need to build a USB E-Brake (Gaming controller)
    By childofbodom16 in forum Tech Board
    Replies: 1
    Last Post: 04-25-2007, 05:34 AM
  5. Replies: 4
    Last Post: 06-30-2004, 03:11 PM