// PIC16F628A Configuration Bit Settings
// 'C' source line config statements
#include <xc.h>
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// CONFIG
#pragma config FOSC = INTOSCIO // Oscillator Selection bits (INTOSC oscillator: CLKOUT function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is digital input, MCLR internally tied to VDD)
#pragma config BOREN = OFF // Brown-out Detect Enable bit (BOD disabled)
#pragma config LVP = OFF // Low-Voltage Programming Enable bit (RB4/PGM pin has digital I/O function, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EE Memory Code Protection bit (Data memory code protection off)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#define _XTAL_FREQ 4e6
int delay = 500;
void MoveLeft()
{
while(RA4!=1)
{
PORTA=PORTA*2;
__delay_ms(delay);
}
}
void MoveRight()
{
while(RA0!=1)
{
PORTA=PORTA/2;
__delay_ms(delay);
}
}
void main()
{
CMCON=0x07;
TRISA=0x00;
PORTA=0b00011111;
__delay_ms(500);
PORTA=0b00000001;
__delay_ms(500);
while(1)
{
if(RA0==1)
MoveLeft();
if(RA4==1)
MoveRight();
}
}
Code C - [expand] 1 2 CMCON = 0x07; VRCON = 0x00;
char portstate = 0;
void MoveLeft()
{
while(portstate < (1<<4))
{
portstate = portstate << 1;
PORTA = portstate ;
__delay_ms(delay);
}
}
void MoveRight()
{
while(portstate > 1)
{
portstate = portstate >> 1;
PORTA = portstate;
__delay_ms(delay);
}
}
void main()
{
CMCON=0x07;
TRISA=0x00;
PORTA=0b00011111;
__delay_ms(500);
PORTA=0b00000001;
__delay_ms(500);
while(1)
{
if(portstate == 0)
MoveLeft();
if(portstate == (1<<4))
MoveRight();
}
}
If you are testing in Proteus I have to tell that some Proteus models have bugs
int delay = 500;
void MoveLeft()
{
while(RA3!=1)
{
PORTA=PORTA*2;
__delay_ms(delay);
}
}
void MoveRight()
{
while(RA0!=1)
{
PORTA=PORTA/2;
__delay_ms(delay);
}
}
void main()
{
CMCON=0x07;
VRCON=0x00;
TRISA=0x00;
PORTA=0b00001111;
__delay_ms(500);
PORTA=0b00000001;
__delay_ms(500);
while(1)
{
if(RA0==1)
MoveLeft();
if(RA3==1)
MoveRight();
}
}
int delay = 500;
void MoveLeft()
{
while(RA4!=1)
{
PORTA=PORTA*2;
__delay_ms(delay);
}
}
void MoveRight()
{
while(RA0!=1)
{
PORTA=PORTA/2;
__delay_ms(delay);
}
}
void main()
{
CMCON=0x07;
VRCON=0x00;
TRISA=0x00;
PORTA=0b00011111;
__delay_ms(500);
PORTA=0b00000001;
__delay_ms(500);
while(1)
{
if(RA0==1)
MoveLeft();
if(RA4==1)
MoveRight();
}
}
int delay = 500;
void main()
{
CMCON=0x07;
VRCON=0x00;
TRISA=0xE0;
PORTA=0b00011111;
__delay_ms(500);
PORTA=0b00000001;
__delay_ms(500);
while(1)
{
PORTA=0b00000010;
__delay_ms(delay);
PORTA=0b00000100;
__delay_ms(delay);
PORTA=0b00001000;
__delay_ms(delay);
PORTA=0b00010000;
__delay_ms(delay);
PORTA=0b00001000;
__delay_ms(delay);
PORTA=0b00000100;
__delay_ms(delay);
PORTA=0b00000010;
__delay_ms(delay);
PORTA=0b00000001;
__delay_ms(delay);
}
}
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 const unsigned long int delay = 1000; short shift = 1; void MoveLeft() { while(shift != 0x10) { PORTA = shift; shift <<= 1; Delay_ms(delay); } } void MoveRight() { while(shift != 0x01) { PORTA = shift; shift >>= 1; Delay_ms(delay); } } void main() { CMCON = 0x07; VRCON = 0x00; TRISA = 0x00; TRISB = 0x00; PORTB = 0x00; PORTA = 0b00011111; Delay_ms(1000); PORTA = 0b00000001; while(1) { MoveLeft(); MoveRight(); } }
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?