Control de Servomotor con Pic 16f84 (codigos de ejemplo)
Codigo1:
#INCLUDE <16F84A.h>
#FUSES XT,NOWDT
#USE delay (clock=4000000)
#USE fast_io(B)
#DEFINE INTS_PER_SECOND 250 // (4000000/(4*4*250))
int int_count,milisegundos,segundos;
#INT_RTCC
Interrupcion_RTCC() {
set_timer0(0);
if(–int_count==0) {
milisegundos++;
if(milisegundos==1000) {
segundos++;
milisegundos==0;
}
int_count=INTS_PER_SECOND;
}
}
void main() {
int_count=INTS_PER_SECOND;
milisegundos = 0;
segundos = 0;
setup_counters(RTCC_INTERNAL,RTCC_DIV_4);
enable_interrupts(INT_RTCC);
enable_interrupts(GLOBAL);
set_timer0(0);
while(1) {
if(segundos==2 || segundos==3) { // IR A 90°
output_high(PIN_B1);
delay_us(1500);
output_low(PIN_B1);
delay_us(18500);
}
if(segundos==4 || segundos==5) { // IR A 180°
output_high(PIN_B1);
delay_us(2100);
output_low(PIN_B1);
delay_us(17900);
}
if(segundos==6 || segundos==7) { // IR A 0°
output_high(PIN_B1);
delay_us(900);
output_low(PIN_B1);
delay_us(19100);
}
if(segundos==8) {
segundos = 0;
}
}
}
Test de servomotor:
Codigo2:
#INCLUDE <16F84A.h>
#FUSES XT,NOWDT
#USE delay (clock=4000000)
#USE fast_io(B)
void main() {
while(1) {
output_high(PIN_B1);
delay_us(1500);
output_low(PIN_B1);
delay_us(18500);
}
}
Codigo3:
#INCLUDE <16F84A.h>
#FUSES XT,NOWDT
#USE delay (clock=4000000)
#USE fast_io(B)
void main() {
set_tris_b(0);
while(1) {
output_high(PIN_B0);
output_high(PIN_B1);
output_high(PIN_B2);
�
delay_us(9);
output_low(PIN_B0);
�
delay_us(6);
output_low(PIN_B1);
delay_us(6);
output_low(PIN_B2);
�
delay_us(179);
}
}
Codigo4:
Para “TowerPro SG90″
*** Seeeduino & Towerpro SG90 Test Program – TL Fong 2009jan08
/******************************************************************************
* Hello Servo – Turn TowerPro SG90 servo to middle position
* Created – 2009jan07, Last update – 2008jan07
* Function – Turn servo to middle position
* MPU/Programming language – Seeeduino v1.1/Arduino v12.0
* Author – TL Fong (tlfong01, TaoBao)
* Copyright – Creative Commons Attribution – ShareAlike 3.0 Licence
*
* Test procedure
* (1) FIRST run the program,
* (2) Manually turn servo clockwise or counterclock to limit,
* (3) Connect 5V power and MPU signal to servo,
* (4) WARNING – Disconnect 5V power before stopping program,
*
* TowerPro SG90 Spec
* CCW end 900uS, CW end 2100uS, middle 1500uS, 3.0 to 6.0V,
* 9g, 22 * 11.5 * 27mm, 0.12sec/60 degrees (4.8V no load),
* stall torque 17.5ozin/1.2 kgcm) (4.8V), dead band 7usec,
* coreless motor, all nylon gear, RMB30 (TaoBao).
*
* Notes
* 1. To turn counter clockwise limit, change pulse width from 1500 to 900.
* 2. To turn clockwise limit, change pulse width from to 2100.
******************************************************************************/
int signalPin = 8; //servo signal pin
void setup()
{
pinMode(signalPin, OUTPUT);
}
void loop()
{
digitalWrite(signalPin, HIGH); // control signal high
delayMicroseconds(1500); // for 1500 uS
digitalWrite(signalPin, LOW); // control signal low
delay(20); // wait 20mS
}
/* End of program */
________________________________________
/******************************************************************************
* Hello Servo – v2.0
* Created – 2009jan07, Last update – 2008jan07
* Function – Repeatedly position servo to one end, middle, and the other end
* Servo – Towerpro SG90
* MPU/Programming language – Seeeduino v1.1/Arduino v12.0
* Author – TL Fong (tlfong01, TaoBao)
* Copyright – Creative Commons Attribution – ShareAlike 3.0 Licence
*
* Test procedure
* (1) FIRST run the program,
* (2) Manually turn servo clockwise or counterclock to limit,
* (3) Connect 5V power and MPU signal to servo,
* (4) WARNING – Disconnect 5V power before stopping program.
*
* TowerPro SG90 Spec
* CCW end 900uS, CW end 2100uS, middle 1500uS, 3.0 to 6.0V,
* 9g, 22 * 11.5 * 27mm, 0.12sec/60 degrees (4.8V no load),
* stall torque 17.5ozin/1.2 kgcm) (4.8V), dead band 7usec,
* coreless motor, all nylon gear, RMB30 (TaoBao).
*
* Notes
* Hold time = 100 * 20mS = 2 seconds
*
* Disclaimer
* Use the program at your own risk. This program is written by a servo newie.
* There is no guarantee that the program will not damage your servo.
******************************************************************************/
int signalPin = 8; //servo signal pin
void setup()
{
pinMode(signalPin, OUTPUT);
}
void loop()
{
for (int i=0; i <= 100; i++) // turn to CCW end, hold 2 seconds
{
digitalWrite(signalPin, HIGH); // control signal high
delayMicroseconds(900); // for 900 uS
digitalWrite(signalPin, LOW); // control signal low
delay(20); // wait 20mS
}
for (int i=0; i <= 100; i++) // turn to middle, hold 2 seconds
{
digitalWrite(signalPin, HIGH); // control signal high
delayMicroseconds(1500); // for 1500 uS
digitalWrite(signalPin, LOW); // control signal low
delay(20); // wait 20mS
}
for (int i=0; i <= 100; i++) // turn to CW end, hold 2 seconds
{
digitalWrite(signalPin, HIGH); // control signal high
delayMicroseconds(2100); // for 1500 uS
digitalWrite(signalPin, LOW); // control signal low
delay(20); // wait 20mS
}
�
for (int i=0; i <= 100; i++) // turn to middle, hold 2 seconds
{
digitalWrite(signalPin, HIGH); // control signal high
delayMicroseconds(1500); // for 1500 uS
digitalWrite(signalPin, LOW); // control signal low
delay(20); // wait 20mS
}
}
/* End of program */
[ENDS]
Codigo5:
#INCLUDE <16F84A.h>
#FUSES XT,NOWDT
#USE delay (clock=4000000)
#USE fast_io(B)
void main() {
int i,segundos;
i = 0;
set_tris_b(0);
segundos = 0;
while(1) {
if(segundos>=0 && segundos<=4) {
output_high(PIN_B4);
delay_us(1500);
output_low(PIN_B4);
delay_us(18500);
i++;
if(i==50) {
segundos++;
i=0;
}
}
if(segundos>=5 && segundos<=10) {
output_high(PIN_B4);
delay_us(2100);
output_low(PIN_B4);
delay_us(17900);
i++;
if(i==50) {
segundos++;
i=0;
}
}
if(segundos>=11 && segundos<=16) {
output_high(PIN_B4);
delay_us(900);
output_low(PIN_B4);
delay_us(19100);
i++;
if(i==50) {
segundos++;
i=0;
}
}
if(segundos==17) {
segundos = 0;
i=0;
}
}
}
Fuente: http://www.todopic.com.ar/foros/index.php?PHPSESSID=4524f168343d6e852d8b4221b5ebad8b&topic=24768.0
Tuitear 16.216 Veces leído 0 comentarios |
11 julio 2010 en Electronica | tags: Electronica |