#include <htc.h>
__CONFIG(UNPROTECT & BORDIS & MCLRDIS & PWRTEN & WDTDIS & INTIO);
void main()
{
unsigned int i;
TRISIO = 0b000011; //set GPIO0-3 as inputs, high Z
ANSEL = 0x00; //clear ansel register
CMCON = 0x07; //disable comparator
while (1){ //loop continuously
GPIO4 = 1; //set pin 3 high, LED on
GPIO5 = 0; //set pin 2 low
for(i = 0; i < 10000; ++i)
{
// do nothing, busy wait loop
}
GPIO4 = 0; //set pin 3 low, LED off
GPIO5 = 0; //set pin 2 low
for(i = 0; i < 10000; ++i)
{
// do nothing, busy wait loop
}
}
}
GPIO4 = 1; //set pin 3 high, LED on
GPIO5 = 0; //set pin 2 low
GPIO4 = 1; //set pin 3 high, LED on
GPIO5= 0; //set pin 2 low
Should be 0b0011110b000011
TRISIO=0x000110
TRISIO=0x000011
Should be 0b000000
if not RA2 will sink for led D2
It is not difficult. You can figure out which lines are hi low or tristated for each LED by looking at the schematic.I can flash the leds in a roll but it don't want to turn on just 1 lol I see why it would drive you cazy
TRISIO=trisioArray[3];
GPIO = gpioArray[3];
#define LED_ON(n) TRISIO=trisioArray[3];GPIO = gpioArray[3]
// to turn on LED2 use
LED_ON(2);
// ADVANCED COMPUTER SCIENCE 9/23/2008
// DEMO PROGRAM
//
// Name: read switches
// Purpose: demostrate how to read switches and the
// use of arrays to operate the LEDs
//
//
// settings for configuration memory
#pragma config OSC = INTIO2, WDT = OFF, LVP = OFF
// processor definitions
#include <p18f1320.h>
// tell the compiler these are unsigned 8 bit
// constants that can be stored in flash with
// the program code.
rom const unsigned char lataVal[] =
{0x01, 0x40, 0x40, 0x80, 0x80, 0x01};
rom const unsigned char trisaVal[] =
{~0x41,~0x41,~0xC0,~0xC0,~0x81,~0x81};
void main(void)
{
unsigned char sw;
unsigned char btn1;
unsigned char btn2;
unsigned char btn3;
// crank up the internal clock to 8MHz
OSCCONbits.IRCF0=1;
OSCCONbits.IRCF1=1;
OSCCONbits.IRCF2=1;
ADCON1=0x70; // all digital
TRISB=0x25; // Switches on RB0 RB2 RB5
// PORTB weak pullups for switches
INTCON2bits.RBPU=0;
while(1)
{
// Read from PORTB,
// what would happen if used
//'sw=PORTB&0x25' instead ?
// Use PORTB not LATB with weak pullups.
sw=PORTB;
// deterimine if any was pressed
btn1 = (!(sw & 0x01)); // RB0
btn2 = (!(sw & 0x04)); // RB2
btn3 = (!(sw & 0x20)); // RB5
// turn on correct LED if switch was pressed
if(btn1) // why do we not use "if(btn1==1)" ?
{
LATA=lataVal[0];
TRISA=trisaVal[0];
}
if(btn2)
{
LATA=lataVal[1];
TRISA=trisaVal[1];
}
if(btn3)
{
LATA=lataVal[2];
TRISA=trisaVal[2];
}
}
}
list p=12f675
include "p12f675.inc"
__CONFIG _CPD_OFF & _CP_OFF & _BODEN_OFF & _MCLRE_OFF &_PWRTE_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT
#define BANK1 banksel 0x80 ;Select BANK1
#define BANK0 banksel 0x00 ;Select BANK0
cblock 0x20 ;start of general purpose registers
count1 ;used in delay routine
counta ;used in delay routine
countb ;used in delay routine
endc
org 0x0000 ;org sets the origin, 0x0000 for the 12f675,
main
call 0x3FF ; retrieve factory calibration value
BANK1
movwf OSCCAL ; update register with factory cal value
BANK0
clrf GPIO ;Clear Port
BANK1
clrf ANSEL
movlw 0xFF
movwf TRISIO ;Tri-State All Inputs
clrf VRCON ;Vref Off
BANK0
movlw 0x07
movwf CMCON ;Comparator Off
start:
bsf STATUS,RP0 ; select Register Page 1
bcf TRISIO,4 ; make IO Pin C0 an output
bcf TRISIO,5
bcf STATUS,RP0 ; back to Register Page 0
bsf GPIO,4 ; turn on LED C0 (DS1)
call Delay ;this waits for a while!
clrf GPIO ;turn off led
call Delay ;this waits for a while!
goto start ; wait here
Delay movlw d'250' ;delay 250 ms (4 MHz clock)
movwf count1
d1 movlw 0xC7
movwf counta
movlw 0x01
movwf countb
Delay_0
decfsz counta, f
goto $+2
decfsz countb, f
goto Delay_0
decfsz count1 ,f
goto d1
retlw 0x00
end
void init_ports(void) {
TRISIO = 0B00001111; // set as output
}
void main() {
init_ports();
while(1) { // infinite loop
GPIO = (1<<4);
delay_ms(200);
GPIO = 0;
delay_ms(200);
}
}
include <htc.h>
__CONFIG(UNPROTECT & BORDIS & MCLRDIS & PWRTEN & WDTDIS & INTIO);
void main()
{
unsigned int i;
TRISIO = 0b001111; //set GPIO0-3 as inputs, high Z
ANSEL = 0x00; //clear ansel register
CMCON = 0x07; //disable comparator
while (1){ //loop continuously
GPIO4 = 1; //set pin 3 high, LED on
for(i = 0; i < 10000; ++i)
{
// do nothing, busy wait loop
}
GPIO4 = 0; //set pin 3 low, LED off
for(i = 0; i < 10000; ++i)
{
// do nothing, busy wait loop
}
}
}
#include <htc.h>
__CONFIG(UNPROTECT & BORDIS & MCLRDIS & PWRTEN & WDTDIS & INTIO);
const unsigned char TrisIOArray[] =
{0b000110, 0b000110, 0b100010, 0b100010, 0b010010, 0b010010, 0b110000, 0b110000};
//Array containing all neccessary TRSIO register values
const unsigned char GpioArray[] =
{0b010000, 0b100000, 0b010000, 0b000100, 0b100000, 0b000100, 0b000100, 0b000010};
//Array containing all neccessary GPIO register values
#define LED_ON(n) TRISIO=TrisIOArray[n];GPIO = GpioArray[n] //define LED_ON() macro
void main()
{
unsigned int i;
unsigned int n;
ANSEL = 0x00; //clear ansel register
CMCON = 0x07; //disable comparator
while (1){ //loop continuously
for(n = 0; n < 8; n++) //cycle through the array values from 0-7
{
LED_ON(n); //call LED_ON() macro, assign variable n to it
for(i = 0; i < 10000; i++)
{
// do nothing, busy wait loop
}
}
}
}
#include <htc.h>
__CONFIG(UNPROTECT & BORDIS & MCLRDIS & PWRTEN & WDTDIS & INTIO);
const unsigned char TrisIOArray[] =
{0b000110, 0b000110, 0b100010, 0b100010, 0b010010, 0b010010, 0b110000, 0b110000};
//Array containing all neccessary TRSIO register values
const unsigned char GpioArray[] =
{0b010000, 0b100000, 0b010000, 0b000100, 0b100000, 0b000100, 0b000100, 0b000010};
//Array containing all neccessary GPIO register values
#define LED_ON(n) TRISIO=TrisIOArray[n];GPIO = GpioArray[n] //define LED_ON() macro
void main()
{
unsigned int i;
unsigned int n;
unsigned int j = 0;
INTCON = 0; //disable interrupts
ANSEL = 0x00; //clear ansel register
CMCON = 0x07; //disable comparator
while (1){ //loop continuously
for(n = 0; n < 8; n++) //cycle through the array values from 0-7
{
LED_ON(n); //call LED_ON() macro, assign variable n to it
j++; //increment j every time loop runs
if (j <= 20)
for(i = 0; i < 10000; i++)
{
// do nothing, slower busy wait loop
}
else
for(i = 0; 1 < 5000; i++)
{
// do nothing, faster busy wait loop
}
}
}
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?