Timer/Counter: Timer 1 Below Are The Steps For Configuring and Using The Timer1 For Delay Generation
Timer/Counter: Timer 1 Below Are The Steps For Configuring and Using The Timer1 For Delay Generation
https://embedded-lab.com/blog/lab-7-timers-and-counters-part-1/
Timer/Counter: Timer 1
Below are the steps for configuring and using the Timer1 for delay generation:
Below is the sample code to blink the LEDs with 100ms delay.
#include<pic16f877a.h>
char value = 0;
#define SBIT_PS1 5
#define SBIT_PS0 4
void main()
{
TRISD=0x00; //COnfigure PORTD as output to blink the LEDs
while(1)
{
PORTD = value;
}
}
#include <xc.h>
#include <stdint.h>
#include <string.h>
TX2REG = txData;
}
void main(void)
{
char msg[] = "Hello World\r\n";
CLK_init();
EUSART2_init();
PPS_init();
PORT_init();
while(1)
{
for(uint8_t i = 0; i < strlen(msg); i++)
{
EUSART2_write(msg[i]);
}
}
}
Result:
12. Interface a Stepper motor and rotate it in clockwise and anti-clockwise direction.
https://circuitdigest.com/microcontroller-projects/interfacing-stepper-motor-with-pic16f877a
Schematic Diagram:
/*
* File: main.c
* By:- circuitdigest.com
* This program will drive a servo motor.
*/
// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit
(RB3/PGM pin has PGM function; low-voltage programming enabled)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection
off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program
memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#include <xc.h>
#include <stdio.h>
/*
Hardware related definition
*/
ms_delay(1000);
}
}
/*This will drive the motor in full drive mode depending on the direction*/
void full_drive (char direction){
if (direction ==
anti_clockwise){PORTB =
0b00000011;
ms_delay(speed);
PORTB = 0b00000110;
ms_delay(speed);
PORTB = 0b00001100;
ms_delay(speed);
PORTB = 0b00001001;
ms_delay(speed);
PORTB = 0b00000011;
ms_delay(speed);
}
if (direction ==
clockwise){PORTB =
0b00001001;
ms_delay(speed);
PORTB = 0b00001100;
ms_delay(speed);
PORTB = 0b00000110;
ms_delay(speed);
PORTB = 0b00000011;
ms_delay(speed);
PORTB = 0b00001001;
ms_delay(speed);
}
/* This method will drive the motor in half-drive mode using direction input */
/* This function will drive the the motor in wave drive mode with direction input*/
void wave_drive (char direction){
if (direction == anti_clockwise){
PORTB = 0b00000001;
ms_delay(speed);
PORTB = 0b00000010;
ms_delay(speed);
PORTB = 0b00000100;
ms_delay(speed);
PORTB = 0b00001000;
ms_delay(speed);
}
if (direction ==
clockwise){PORTB =
0b00001000;
ms_delay(speed);
PORTB = 0b00000100;
ms_delay(speed);
PORTB = 0b00000010;
ms_delay(speed);
PORTB = 0b00000001;
ms_delay(speed);
}