0% found this document useful (0 votes)
21 views

Timer/Counter: Timer 1 Below Are The Steps For Configuring and Using The Timer1 For Delay Generation

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Timer/Counter: Timer 1 Below Are The Steps For Configuring and Using The Timer1 For Delay Generation

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

10.

Implementation of Timer/Counter using PIC microcontroller

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:

1. Calculate the Timer Count for the required delay.


2. Set the Presaclar bits in T1CON as per the delay calculations.
3. Select the Clock Source Internal/External using TMR1CS bit.
4. Load the timer value into TMR1H,TMR1L register.
5. Enable the Timer1 Interrupt by setting TMRIE bit
6. Enable the Global and Peripheral interrupts by setting GIE and PIE bits
7. Finally start the timer by setting TMR1ON bit

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 interrupt timer_isr()


{ if(TMR1IF==1
)
{
value=~value; // complement the value for blinking the LEDs
TMR1H=0x0B; // Load the time value(0xBDC) for 100ms delay
TMR1L=0xDC;
TMR1IF=0; // Clear timer interrupt flag
}
}

void main()
{
TRISD=0x00; //COnfigure PORTD as output to blink the LEDs

T1CON = (1<<SBIT_PS1) | (1<<SBIT_PS0); // Timer0 with external freq and 8 as prescalar


TMR1H=0x0B; // Load the time value(0xBDC) for 100ms delay
TMR1L=0xDC;
TMR1IE=1; //Enable timer interrupt bit in PIE1 register
GIE=1; //Enable Global Interrupt
PEIE=1; //Enable the Peripheral Interrupt
TMR1ON = 1; //Start Timer1

while(1)
{
PORTD = value;
}
}

Delay measured using CRO:


11. Display “Hello World” message using Internal UART

/* Disable Watch Dog Timer */


#pragma config WDTE = OFF
/* Low voltage programming enabled , RE3 pin is MCLR */
#pragma config LVP = ON

#include <xc.h>
#include <stdint.h>
#include <string.h>

static void CLK_init(void);


static void EUSART2_init(void);
static void PPS_init(void);
static void PORT_init(void);
static void EUSART2_write(uint8_t txData);

static void CLK_init(void)


{
/* Set HFINTOSC as new oscillator source */
OSCCON1bits.NOSC = 0b110;

/* Set HFFRQ to 1 MHz */


OSCFRQbits.HFFRQ = 0;
}

static void EUSART2_init(void)


{
/* Transmit Enable */
TX2STAbits.TXEN = 1;
/* High Baud Rate Select */
TX2STAbits.BRGH = 1;

/* 16-bit Baud Rate Generator is used */


BAUD2CONbits.BRG16 = 1;

/* Baud rate 9600 */


SP2BRGL = 25;
SP2BRGH = 0;

/* Serial Port Enable */


RC2STAbits.SPEN = 1;
}

static void PPS_init(void)


{
/* RD0 is TX2 */
RD0PPS = 0x0B;
}

static void PORT_init(void)


{
/* Configure RD0 as output. */
TRISDbits.TRISD0 = 0;
}

static void EUSART2_write(uint8_t txData)


{
while(0 == PIR3bits.TX2IF)
{
;
}

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.
*/

// PIC16F877A Configuration Bit Settings

// 'C' source line config statements

// 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
*/

#define _XTAL_FREQ 200000000 //Crystal Frequency, used in delay


#define speed 1 // Speed Range 10 to 1 10 = lowest , 1 = highest
#define steps 250 // how much step it will take
#define clockwise 0 // clockwise direction macro
#define anti_clockwise 1 // anti clockwise direction macro
/*
*Application related function and definition
*/

void system_init (void); // This function will initialise the ports.


void full_drive (char direction); // This function will drive the motor in full drive mode
void half_drive (char direction); // This function will drive the motor in full drive mode
void wave_drive (char direction); // This function will drive the motor in full drive mode
void ms_delay(unsigned int val);
/*
* main function starts here
*/
void main(void)
{
system_init();
while(1){
/* Drive the motor in full drive mode clockwise */
for(int i=0;i<steps;i++)
{
full_drive(clockwise);
}
ms_delay(1000);

/* Drive the motor in wave drive mode anti-clockwise */


for(int i=0;i<steps;i++)
{
wave_drive(anti_clockwise);
//full_drive(anti_clockwise);

ms_delay(1000);
}
}

/*System Initialising function to set the pin direction Input or Output*/


void system_init (void){
TRISB = 0x00; // PORT B as output port
PORTB = 0x0F;
}

/*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 */

void half_drive (char direction){


if (direction ==
anti_clockwise){PORTB =
0b00000001;
ms_delay(speed);
PORTB = 0b00000011;
ms_delay(speed);
PORTB = 0b00000010;
ms_delay(speed);
PORTB = 0b00000110;
ms_delay(speed);
PORTB = 0b00000100;
ms_delay(speed);
PORTB = 0b00001100;
ms_delay(speed);
PORTB = 0b00001000;
ms_delay(speed);
PORTB = 0b00001001;
ms_delay(speed);
}
if (direction ==
clockwise){PORTB =
0b00001001;
ms_delay(speed);
PORTB = 0b00001000;
ms_delay(speed);
PORTB = 0b00001100;
ms_delay(speed);
PORTB = 0b00000100;
ms_delay(speed);
PORTB = 0b00000110;
ms_delay(speed);
PORTB = 0b00000010;
ms_delay(speed);
PORTB = 0b00000011;
ms_delay(speed);
PORTB = 0b00000001;
ms_delay(speed);
}
}

/* 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);
}

You might also like