Note: The ANSEL register must be initialized to
configure an analog channel as a digital
input. Pins configured as analog inputs will
read ‘0’.
PORTA = 0x00;
ADCON1 = 0x06;
TRISA = 0b11111111; or TRISA = 0xFF;
PORTB = 0x00;
TRISB=0b11111111; or TRISB = 0xFF;
PORTD = 0x00;
TRISD=0b00000000; or TRISD = 0x00;
//speeding;//
if(RA4==0){
while(RA4==0)continue;
counter++;
}
//un speeding//
if(RB0==0){
while(RB0==0)continue;
counter--;
}
why do i need to initialize them? i think it work without them just fine
continue does nothing,just like ; continue;=; =)
I know its working on simulator,but would be there some problems in real hardware because of cpu ond __delay_us()?
//==========================================================================
// Project : Sample code for control single servo motors using delays
// This project is using 16F84A with 4MHz
// Has been try succesfully using GWS S03N STD
//
// Please make sure the hardware is compatible with this code
//
// Project description : This source code is used to control a servo motor.
// Servo motor will continuosly turn from one position to another position
// Lib func __delay_us() and __delay_ms() is use to generate the pulse width.
// The delay timing is independent of crystal but_XTAL_FREQ must be set correctly.
//
//
// Author : BigDogGuru
// Date : April 2011
//==========================================================================
//
// include
//==========================================================================
#include "config.h"
#include "delays.h"
#include <htc.h> // this sample code is using 16F84A !!
// configuration
//==========================================================================
__CONFIG ( 0x3FF2 ); // configuration for the 16F84A
// define
//==========================================================================
#define servo RB5 // servo set RB5
// function (every function must have a function prototype)
//==========================================================================
// none
//
// main function (main fucntion of the program)
//==========================================================================
void main(void)
{
unsigned int i;
//set IO port for servo pin
TRISB = 0b00000000; //servo motor is RB1
PORTB = 0b00000000; //clear port B
//servo will loop infinity
while(1) //from one position to another position
{
//*******************************************************
//
//Delay determine the servo motors position
//Try change the value for different position
//
//Value in __delay_xs() function is unsigned long
//Calculate the timing correctly or servo damage could result!!
//
//******************************************************
for(i=0;i<50;i++) //continues pulse for 50 times
{
servo=1; //set servo pin high
delay_us(900); //delay 0.7ms
servo=0; //set servo pin low
delay_us(19100); //delay 19.3ms
}
// _ _ _
// | | | | | |
// | | | | | | ~~50 times
// | |_________________| |________________| |____________________
// 0.7ms 19ms 0.7ms 19ms 0.7ms 19ms
// | |
// |<-------20ms------>|
for(i=0;i<50;i++) //continues pulse for 50 times
{
servo=1; //set servo pin high
delay_us(1500); //delay 1.8ms
servo=0; //set servo pin low
delay_us(18500); //delay 18.2ms
}
for(i=0;i<50;i++) //continues pulse for 50 times
{
servo=1; //set servo pin high
delay_us(2000); //delay 1.8ms
servo=0; //set servo pin low
delay_us(18000); //delay 18.2ms
}
// ___ ___ ___
// | | | | | |
// | | | | | | ~~50 times
// | |_______________| |_______________| |____________________
// 2ms 18ms 2ms 18ms 2ms 18ms
// | |
// |<-------20ms------>|
}
}
//subroutines
//============================================================================
//
// none
This programm does LED flashnih from 0% to 100% in about 1 second,what should I change to LED flashing from 0% to 100% ten times longer?
while( 1 ) // beskonacna petlja pocetak
{
for(i=0;i<50;i++)
{
RB4 = 1; // Upali puls na LED
for(pulse = 0; pulse < duty; pulse++); // Puls postoji dok je manji od duty-a
RB4 = 0; // Gasi puls na LED
for(pulse = 0; pulse < (freq - duty); pulse++); // Continue until frequency
}
duty++; // povecava duty za 1 i krece ispocetka s vecim duty-em
}
#include <htc.h>//OK
#define _XTAL_FREQ 4000000
__CONFIG(FOSC_HS & WDTE_OFF & PWRTE_OFF & CP_OFF & BOREN_OFF & LVP_OFF & CPD_OFF & WRT_OFF);
unsigned char pulse = 0; // Puls pocinje od nule
unsigned char duty = 0; // Duty cycle
unsigned char freq = 255; // Frequencija
unsigned char i;
void main()
{
// inicijalizacija
TRISB=0x00; // RB oznaceni kao izlazi
TRISA=0xFF; // RA kao ulazi
while( 1 ) // beskonacna petlja pocetak
{
for(i=0; i<25; i++)
{
RB4 = 1; // Upali puls na LED
for(pulse = 0; pulse < duty; pulse++); // Puls postoji dok je manji od duty-a
RB4 = 0; // Gasi puls na LED
for(pulse = 0; pulse < (freq - duty); pulse++); // Continue until frequency
}
duty++; // povecava duty za 1 i krece ispocetka s vecim duty-em
}
}
C:\Program Files\HI-TECH Software\PICC\9.81\include
//
// Configuration mask definitions
//
// Config Register: CONFIG
#define CONFIG 0x2007
// Oscillator Selection bits
// RC oscillator
#define FOSC_EXTRC 0xFFFF
// HS oscillator
#define FOSC_HS 0xFFFE
// XT oscillator
#define FOSC_XT 0xFFFD
// LP oscillator
#define FOSC_LP 0xFFFC
// Watchdog Timer Enable bit
// WDT enabled
#define WDTE_ON 0xFFFF
// WDT disabled
#define WDTE_OFF 0xFFFB
// Power-up Timer Enable bit
// PWRT disabled
#define PWRTE_OFF 0xFFFF
// PWRT enabled
#define PWRTE_ON 0xFFF7
// FLASH Program Memory Code Protection bits
// Code protection off
#define CP_OFF 0xFFFF
// 1F00h to 1FFFh code protected
#define CP_UPPER_256 0xEFEF
// 1000h to 1FFFh code protected
#define CP_HALF 0xDFDF
// 0000h to 1FFFh code protected
#define CP_All 0xCFCF
// Brown-out Reset Enable bit
// BOR enabled
#define BOREN_ON 0xFFFF
// BOR disabled
#define BOREN_OFF 0xFFBF
// Low Voltage In-Circuit Serial Programming Enable bit
// RB3/PGM pin has PGM function; low-voltage programming enabled
#define LVP_ON 0xFFFF
// RB3 is digital I/O, HV on MCLR must be used for programming
#define LVP_OFF 0xFF7F
// Data EE Memory Code Protection
// Code Protection off
#define CPD_OFF 0xFFFF
// Data EEPROM memory code-protected
#define CPD_ON 0xFEFF
// FLASH Program Memory Write Enable
// Unprotected program memory may be written to by EECON control
#define WRT_ON 0xFFFF
// Unprotected program memory may not be written to by EECON control
#define WRT_OFF 0xFDFF
// In-Circuit Debugger Mode bit
// In-Circuit Debugger disabled, RB6 and RB7 are general purpose I/O pins
#define DEBUG_OFF 0xFFFF
// In-Circuit Debugger enabled, RB6 and RB7 are dedicated to the debugger
#define DEBUG_ON 0xF7FF
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?