Thread: Bit to Byte?

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    5

    Bit to Byte?

    I've written the following program in bit instructions. How would I do the same program but using byte instructions?

    Thanks for any help and time given.

    /*
    A program that transfers the state of the lower
    5 DIL switches(RC0 - RC4) to the LED's (RA0 - RA4).(Byte Intructions)
    */

    #include <p18f2221.h>
    #pragma config OSC=HS /* 0.4us cycle time with 10MHz osc */
    #pragma config WDT=OFF
    #define Switch1 PORTCbits.RC0
    #define LED1 PORTAbits.RA0
    #define Switch2 PORTCbits.RC1
    #define LED2 PORTAbits.RA1
    #define Switch3 PORTCbits.RC2
    #define LED3 PORTAbits.RA2
    #define Switch4 PORTCbits.RC3
    #define LED4 PORTAbits.RA3
    #define Switch5 PORTCbits.RC4
    #define LED5 PORTAbits.RA4

    void main(void){
    ADCON1=0x0f; /* digital IO */
    TRISA=0xe0; /* 5 LEDs as outputs */
    TRISC=0xff; /* PORTC as inputs */
    PORTA=0x00; /* all LEDs off */

    while(1){ /* forever */
    if(Switch1) /* is switch open (non zero)*/
    LED1=1;
    else
    LED1=0; /* turn LED on */

    while(1){ /*Second Switch */
    if(Switch2)
    LED2=1;
    else
    LED2=0;

    while(1){ /*Third Switch */
    if(Switch3)
    LED3=1;
    else
    LED3=0;

    while(1){ /*Fourth Switch */
    if(Switch4)
    LED4=1;
    else
    LED4=0;

    while(1){ /*Fifth Switch */
    if(Switch5)
    LED5=1;
    else
    LED5=0;
    }
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    The following link seems to have a good basic introduction to the Boolean operators.
    bit twiddling

    But as Tater pointed out please use code tags when posting code.

    Jim

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Similar to the hex constants used for PORTA:
    Code:
    PORTC = 0x01;
    PORTA = 0x00;
    
    if (PORTC) /* PORTCbits.RC0 == 1*/ 
        PORTA=0x01;
    else 
        PORTA=!PORTA;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bit value generation doubt
    By roaan in forum C Programming
    Replies: 2
    Last Post: 08-07-2009, 02:23 PM
  2. Help! I am going crazy. Bit shifting a byte array
    By MrSteve in forum C Programming
    Replies: 7
    Last Post: 12-01-2008, 11:35 PM
  3. bit patterns of negtive numbers?
    By chunlee in forum C Programming
    Replies: 4
    Last Post: 11-08-2004, 08:20 AM
  4. byte and bit order question
    By chunlee in forum C Programming
    Replies: 7
    Last Post: 11-07-2004, 01:50 PM
  5. error: identifier "byte" is undefined.
    By Hulag in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2003, 05:46 PM