Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
2.1 kB
3
Indexable
Never
#include <xc.h>
#include <libpic30.h>

// PIC24FJ128GA010 Configuration Bit Settings
// CONFIG2
#pragma config POSCMOD = XT           // XT Oscillator mode selected
#pragma config OSCIOFNC = ON          // OSC2/CLKO/RC15 as port I/O (RC15)
#pragma config FCKSM = CSDCMD         // Clock Switching and Monitor disabled
#pragma config FNOSC = PRI            // Primary Oscillator (XT, HS, EC)
#pragma config IESO = ON              // Int Ext Switch Over Mode enabled
// CONFIG1
#pragma config WDTPS = PS32768       // Watchdog Timer Postscaler (1:32,768)
#pragma config FWPSA = PR128         // WDT Prescaler (1:128)
#pragma config WINDIS = ON           // Watchdog Timer Window Mode disabled
#pragma config FWDTEN = OFF          // Watchdog Timer disabled
#pragma config ICS = PGx2            // Emulator/debugger uses EMUC2/EMUD2
#pragma config GWRP = OFF           // Writes to program memory allowed
#pragma config GCP = OFF            // Code protection is disabled
#pragma config JTAGEN = OFF        // JTAG port is disabled

#include "buttons.h"


void printBinary(unsigned char n) {
    for (int i = 7; i >= 0; i--)
        printf("%d", (n >> i) & 1);
    printf("\n");
}

void snakeMovement() {
    unsigned char snake = 0b00000111;
    int direction = 1;

    while (1) {
        printBinary(snake);
        if ((snake == 0b00000111 && direction == -1) || (snake == 0b11100000 && direction == 1))
            direction *= -1;
        snake = (direction == 1) ? (snake << 1) : (snake >> 1);
        (500000);
    }
}

int main(void) {
    unsigned char portValue;
    // Port A access
    AD1PCFG = 0xFFFF; // set to digital I/O (not analog)
    TRISA = 0x0000; // set all port bits to be output
    while(1) {
    portValue = 0x55;
    LATA = portValue; // write to port latch
    // delay value change
    __delay32(1500000); // delay in instruction cycles
    portValue = 0xAA;
    LATA = portValue; // write to port latch
    __delay32(1500000); // delay in instruction cycles
    }
    return -1;
}
Leave a Comment