C8051F330/320 Delay with Timer Function not working - c

I am using the Silicon Labs 8051 MCUs. Below is the Delay function with Timer from the examples that came with the IDE.
//-----------------------------------------------------------------------------
// T0_Wait_ms
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters :
// 1) unsigned char ms - number of milliseconds to wait
// range is full range of character: 0 to 255
//
// Configure Timer0 to wait for <ms> milliseconds using SYSCLK as its time
// base.
//
void T0_Wait_ms (unsigned char ms)
{
TCON &= ~0x30; // Stop Timer0; Clear TF0
TMOD &= ~0x0f; // 16-bit free run mode
TMOD |= 0x01;
CKCON |= 0x04; // Timer0 counts SYSCLKs
while (ms) {
TR0 = 0; // Stop Timer0
TH0 = -(SYSCLK/1000 >> 8); // Overflow in 1ms
TL0 = -(SYSCLK/1000);
TF0 = 0; // Clear overflow indicator
TR0 = 1; // Start Timer0
while (!TF0); // Wait for overflow
ms--; // Update ms counter
}
TR0 = 0; // Stop Timer0
}
I changed the input parameters from char to integer to give longer delays.when i checked its not giving accurate delays

Related

Controlling Timer0 in ATmega328p

I'm trying to create a blocking delay for the ATmega328p for my arduino uno R3. but I'm running into problems with my function.
I am not sure if it has something to do with my clock source. I set it as 8 MHz at the top of my script, but the problem persists.
#define F_CPU 8000000UL
Here's a snip of my delay function, it's configured for a 10 ms delay at 8 MHz and uses a delayTime counter to create a longer delay
/*
* Function to instantiate timer operations
*/
void makeDelay(int delayTime){
// Call the timer for delayTime successively
// resulting in delay of (configured delay) * timerDelay
while(delayTime > 0){
/* Set the TCNT reg for 10ms # 8 MHz */
TCNT0 = 0xB2;
// Define the mode and prescaler values
TCCR0A = 0x00; // Normal mode
TCCR0B = 0x05; // prescaler = 1024
// loop until overflow
while( (TIFR0 & (1 << TOV0) == 0) );
// Stop the timer
TCCR0B = 0;
// clear the overflow
TIFR0 = 0x01;
// decrement the counter
delayTime--;
}
}
Any advice would be appreciated.
In your program this waiting condition is wrong:
(TIFR0 & (1 << TOV0) == 0)
Operator == has higher precedence than &. (1 << TOV0) == 0 is evaluated first and it is always false. Thus TIFR0 & 0 is always false too.
Rewrite the condition as
((TIFR0 & (1 << TOV0)) == 0)
or
((TIFR0 & _BV(TOV0)) == 0)
You can use following formula to calculate TCCNT0 register value for milliseconds:
Nticks = 256 - ms*F_CPU/1000/prescaler
As Timer0 is used by Arduino runtime library, you should disable it at first.
Entire sketch may be like this:
#define MS2TICKS(ms) (256 - (ms)*(F_CPU/1000)/1024)
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
TCCR0B = 0; // Stop timer
TIMSK0 = 0; // Disable timer interrupts
TCCR0A = 0; // Normal mode
TIFR0 = ~0; // Clear all interrupt flags
}
void my_delay()
{
TCNT0 = MS2TICKS(10);
TCCR0B = 0x05; // Enable timer with prescaler 1024
while ((TIFR0 & _BV(TOV0)) == 0);
TCCR0B = 0; // Stop Timer0
TIFR0 = _BV(TOV0); // Clear overflow flag
}
void loop()
{
digitalWrite(LED_BUILTIN, HIGH);
my_delay();
digitalWrite(LED_BUILTIN, LOW);
my_delay();
}
It generates 10 ms pulses with 10 ms pauses on LED output.

Can I enable a button toggle interrupt to pause my timer? MSP432 -- Code Composer Studio

Hello! I am working on a project where I have to enable a timer that counts up every tenth of a second and outputs my times in binary. Independently, the timer itself works perfectly. However, I want to add a function to use my internal board switches (P1.4 and P1.1) to stop and start my timer, respectively. In any case, I am working on a MSP432P401R board. Thank you beforehand! My code:
#include "msp.h"
int sec = 0, tensec = 0, min = 0; //time stuff
int A = 1;
void Initiate(){
//internal switches -- negative logic, lazy to do outside stuff
//1.1 -- start related stuff
//1.4 -- stop relted stuff
P1->DIR &= ~0x12;
P1->REN |= 0x12;
P1->OUT |= 0x12;
//asking for start LED
P1->DIR |= BIT0;
P1->OUT &= ~BIT0;
//lights for status -- onboard
P2->DIR |= 0x07;
P2->OUT &= ~0x07;
//minute lights
P5->DIR |= 0x03;
P5->OUT &= ~0x03;
//seconds lights -- 6 LEDs
P4->DIR |= 0x3F;
P4->OUT &= ~0x3F;
//tens of sec
P6->DIR |= BIT0;
P6->OUT &= ~BIT0;
}
int main(void) {
WDT_A->CTL = WDT_A_CTL_PW | WDT_A_CTL_HOLD; // Stop WDT
// Configure GPIO
Initiate();
// Enable & configure the SysTick Timer Module
SysTick->CTRL |= SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_ENABLE_Msk;
// SysTick->LOAD = 0x60000 - 1; // Period = 0x60000
SysTick->LOAD = 300000; // Period = tenth of a second
SysTick->VAL = 0x01; // Clear value by writing any value
SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk; // Enable interrupt
// Enable global interrupt
__enable_irq();
SCB->SCR |= SCB_SCR_SLEEPONEXIT_Msk; // Sleep on exit from ISR
__DSB(); // Ensure SLEEPONEXIT takes effect immediately
while (1)
__sleep();
} // End of main
// Interrupt service routine (ISR) invoked when SysTick down counter reaches 0.
void SysTick_Handler(void)
{
A = P1->IN & BIT1;
if((!A)){ //pressing the switch will make it pause
}else{
P6->OUT ^= BIT0; // Toggle tens of sec light
tensec++;
if(tensec == 10){
sec++;
P4->OUT &=~0x3F; //refresh seconds
P4->OUT |= sec;
tensec = 0; //restart it
if(sec == 60){
sec = 0; //restart secs
min++;
P5->OUT &= ~0x3F; //refresh
P5->OUT |= min;
}
}
}
}

PIC Programming Counter and Touch Sensor Issue

I am new to this PIC programming and currently using C to program because ASM would take a bit time to learn. However, it seems like i am running into the most basic issue of all. The counter i setup with TMR0 is not working correctly and touch sensor does not seem to does anything which it supposed to reset the counter. Can anyone tell me where I did wrong? Been stuck on this for quite a bit. Thanks!
for PIC10F320
[Update]
Hi All, the thanks to the error caught by Kozmotronik. Recalculation was done + reassign bits as global interrupt seems to overflow for some reason. Also option_reg was taken out since T0CS was a 8 bit timer by default. simulation done in proteus for 10secs and timer looks fine now!
The only thing left is the reset from the touch sensor i am still figuring out. Attached a photo from my circuit and temporality replaced the touch sensor with normal button as there is no model for touch sensor. Please take a look if possible.Proteus circuit
// CONFIG
#pragma config FOSC = INTOSC // Oscillator Selection
#pragma config BOREN = OFF // Brown-out Reset
#pragma config WDTE = OFF // Watchdog Timer
#pragma config PWRTE = OFF // Power-up Timer
#pragma config MCLRE = OFF // MCLR Pin Function Select bit->MCLR pin function is digital input, MCLR internally tied to VDD
#pragma config CP = OFF // Code Protection
#pragma config LVP = OFF // Low-Voltage Programming
#pragma config LPBOR = OFF // Brown-out Reset Selection bits
#pragma config BORV = LO // Brown-out Reset Voltage Selection
#pragma config WRT = OFF // Flash Memory Self-Write Protection
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#include <pic10f320.h>
#define _XTAL_FREQ 8000000
#define SWITCH PORTAbits.RA2
#define PWMPin PORTAbits.RA1
#define LEDPin PORTAbits.RA0
unsigned long cnt;
/*
Main application
*/
void setup(void)
{
/**
LATx registers
*/
LATA = 0x00;
/**
TRISx registers
*/
TRISA = 0x04;
/**
ANSELx registers
*/
ANSELA = 0x00;
//Clear out the duty cycle registers
PR2 = 255;
T2CONbits.T2CKPS = 0b00;
T2CONbits.TMR2ON = 0x01;
/*
* My Calculations:
* At 8MHz, Tosc is 125ns, Tcyc=4*Tosc=500ns.
* With PR2=52decimal, prescaler=1, the PWM period is Tcyc(PR2+1) = 500ns*53 = 26.5us.
* For 100% duty cycle,
* PulseWidth = PWMxDCH/L (10bits) * Tosc * 1(prescaler) giving
* 26.5us/125ns=PWMxDCH/L = 212decimal
* PWMxDCH = d4h
* PWMxDCL = 00h
*
* For 75% duty cycle, the duty cycle registers must count off 26.5us*0.75=19.9us.
* PulseWidth = PWMxDCH/L (10bits) * Tosc * 1(prescaler) giving
* 19.9us/125ns=PWMxDCH/L = 159decimal
* PWMxDCH = 9fh
* PWMxDCL = 00h
*/
//Setting timer-0 for the generation of the delay
TMR0 = 0xFF; //Set TMR0 to 0
// OPTION_REG = 0x07;
//INTCON = 0xA0;
> INTCONbits.TMR0IF = 0; //Clear the TMR0 interrupt flag
> INTCONbits.TMR0IE = 1;
OPTION_REGbits.T0CS = 0; //Set TMR0 Clock source to FOS
OPTION_REGbits.PSA = 1; //Assign No-Prescaler to TMR0
/*
* My formulas is:
Fosc/4 = 8MHZ/4 = 2MHZ
period for a tick= 1 / 2MHZ = 0.0000005s
time for 8 bit count = 0.0000005 * 256 = 0.000128s
*
* Overflows every 128us
* for one second delay loop cnt = 1000000*(1/128) = 8
* LC = 7812.5*60 = 480 for 1 minute
* LC = 468750*60 = 28800 for 1 hour
* LC = 28125000*12 = 345600 for 12 hours
*/
}
void main(void)
{
setup();
cnt = 0;
//OSCCON = 2;
while(1){
PWMPin = 1;
LEDPin = 1;
if(SWITCH==1){
cnt++;
}
if(INTCONbits.TMR0IF){
cnt++; //Reset the timer
//100% Duty Cycle on RA1
> TMR0 = 0 ; /// reset TMR0 value
INTCONbits.TMR0IF=0; //Reset Timer Interrupt Flag
}
//If 10sec minute limit is reached (change this number to change the delay)
if(cnt>78125){
//75% Duty Cycle on RA1
// PWM1POL active_hi; PWM1OE enabled; PWM1EN enabled;
PWM2CON = 0xC0;
PWM1CONbits.PWM1OE = 0x01; //PWM1 Turn on
PWM1CONbits.PWM1EN = 0x01; //PWM1 Enable Output
PWM2DCH = 0x00;
PWM2DCL = 0x00;
PWM1DCH = 0x00;
PWM1DCL = 0x00;
PWM2DCH = 0xBF;
PWM2DCL = 0x00;
//LED breathing effect
for(int i=0;i<212;i++){
PWM1DCH = i;
__delay_ms(25);
}
for(int i=212;i>0;i--){
PWM1DCH = i;
__delay_ms(25);
}
}
}
}
Your timer overflow formula seems to be wrong.
Overflows every 128ms
is not true, I think you confused here. Time for 8 bit count is 0.000128s which is 128 microseconds for each timer overflow. So your loop count must be:
for one second delay loop cnt = 1000000*(1/128) = 7812,5
You must re-calculate the rest based on this corrected value. For the touch input you must ensure that you receive proper touch signals because we cannot diagnose it without seeing.

attiny85 bit bang uart with arduino

I have tried to implement TX only uart for ATTiny85 and receive the bits using arduino micro. (similiar question did not help since it is quite different to my situation)
My intend is to be able to print via attiny85 -> arduino -> console so I can debug the attiny85, since I don't have oscilloscope available.
attiny85 fuses are: "efuse:w:0xff:m -U hfuse:w:0xdf:m -U lfuse:w:0xf1:m" aka. 16MHz F_CPU
arduino also seems to have 16MHz F_CPU
Similiar to the mentioned question attiny85 sends bits via timer0 ISR one bit at time:
#ifndef F_CPU
#define F_CPU 16000000UL // 16 MHz
#endif
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#define TX_PIN PB0
volatile uint16_t tx_shift_reg = 0;
ISR(TIMER0_COMPA_vect) {
uint16_t local_tx_shift_reg = tx_shift_reg;
if( local_tx_shift_reg & 0x01 ) {
PORTB |= _BV(TX_PIN);
} else {
PORTB &= ~_BV(TX_PIN);
}
local_tx_shift_reg >>= 1;
if(!local_tx_shift_reg) {
// Stop timer0.
GTCCR |= (1<<TSM) | (1<<PSR0);
}
tx_shift_reg = local_tx_shift_reg;
}
void UART_tx(char byte) {
uint16_t local_tx_shift_reg = tx_shift_reg;
local_tx_shift_reg = (0b1<<9) | ((uint16_t)byte<<1);
tx_shift_reg = local_tx_shift_reg;
TCNT0 = 0;
TCCR0B |= (1<<CS02)|(1<<CS00); // 1024 prescaler
GTCCR &= ~(1<<TSM);
}
void UART_tx_char(char c) {
UART_tx( c );
// wait until transmission is finished
while(tx_shift_reg);
}
void UART_init() {
cli()
// set TX pins as output
DDRB |= (1<<TX_PIN);
PORTB |= (1<<TX_PIN);
// set timer0 to CTC mode, keep it halted.
TCCR0A |= (1<<WGM01);
TCCR0B = 0;
// enable interrupt
TIMSK |= (1<<OCIE0A);
OCR0A = 128;
OCR0B = 128;
TCNT0 = 0;
GTCCR |= (1<<TSM);
sei();
}
void main(void)
{
UART_init();
while(1) {
for(char c = 1; c < 128; ++c) {
UART_tx_char(c);
_delay_ms(100);
}
}
}
Then arduino receives the bits:
/*
* ATtiny85 bit-bang uart monitor for ATmega32u4
*/
#define LED_PIN 13
#define RX_PIN 7
// rx_state == 0 // timer1 not running
// rx_state == 1 // receive in progress
// rx_state == 2 // data ready in rx_data_reg
volatile int rx_state = 0;
volatile int rx_bit_nro = 0;
volatile uint16_t rx_shift_reg = 0;
volatile uint16_t rx_data_reg = 0;
void rx_start_interupt() {
if(rx_state == 0) {
digitalWrite(LED_PIN, HIGH);
while(digitalRead(RX_PIN));
// Start timer1
rx_state++;
TCNT1 = 0;
TCCR1B = (1 << WGM12) | (1 <<CS12) | (1 << CS10);
}
}
ISR(TIMER1_COMPA_vect) {
uint16_t bit = digitalRead(RX_PIN) > 0;
rx_shift_reg >>= 1;
rx_shift_reg |= (bit << 7);
++rx_bit_nro;
if(rx_bit_nro == 9) {
// Stop timer.
TCCR1B = 0;
TCNT1 = 0;
rx_data_reg = rx_shift_reg;
rx_shift_reg = 0;
rx_bit_nro = 0;
rx_state++;
digitalWrite(LED_PIN, LOW);
}
}
void setup() {
noInterrupts();
// Program timer1 for UART bit bang receive.
TCCR1A = 0; // set entire TCCR1A register to 0 (stops it)
TCCR1B = 0; // same for TCCR1B
TCNT1 = 0; // initialize counter value to 0
OCR1A = 128;
TIMSK1 |= (1 << OCIE1A);
interrupts();
pinMode(LED_PIN, OUTPUT);
pinMode(RX_PIN, INPUT);
// Attach RX start interupt.
pinMode(digitalPinToInterrupt(RX_PIN), INPUT);
attachInterrupt(digitalPinToInterrupt(RX_PIN), rx_start_interupt, FALLING);
Serial.begin(9600);
while(!Serial);
Serial.print("F_CPU:");
Serial.println(F_CPU,DEC);
Serial.println("Waiting for data from attiny85...");
}
void loop() {
if(rx_state == 2) {
uint16_t local_rx_data = rx_data_reg;
rx_state = 0;
Serial.println(local_rx_data,HEX);
}
}
I have tried pretty much everything to make it work, but received bytes come back garbace.
What am I missing?
Note: I'm using timer0 on attiny85 and timer1 on arduino.
Solved: switching ISR to TIMER1_COMPAB_vect and OCR1B = 64 actually works! Yay!
I recently ran into this issue of serial comms coming out as garbage on the other end. In my case, the ATtiny85 was sending bits to my laptop using an USB to TTL UART converter which had worked beautifully in other situations, but, I was just getting garbage in the Arduino IDE serial monitor.
I found a forum post which mentioned the possibility of calibrating OSCCAL.
I get a little bit fancier in my related blog post, but I tested the theory that I should calibrate OSCCAL using this code:
#include <SoftwareSerial.h>
SoftwareSerial comm(-1, 0);
static const int anchor = 128;
void
print_osccal(int v) {
comm.println(F("********************************"));
comm.print(F("OSCCAL = "));
comm.println(v);
comm.println(F("********************************"));
}
void
setup() {
delay(5000);
comm.begin(300);
OSCCAL = anchor;
print_osccal(anchor);
delay(5000);
}
void
loop() {
int x;
for (int i = 1; i < 128; ++i) {
x = anchor + i;
OSCCAL = x;
print_osccal(x);
delay(1000);
x = anchor - i;
OSCCAL = x;
print_osccal(x);
delay(1000);
}
}
It was a revelation seeing garbage all of a sudden transform to nicely formatted messages in the serial monitor (and, of course, back to garbage as the search is an infinite loop).
Now, the ATtiny85's internal oscillator can only support 1 MHz and 8 MHz. As I understand it, OSCCAL exists because this internal oscillator is 1) not very accurate and 2) is very sensitive the temperature.
If the ATtiny85 is set to run at 16MHz, it needs a reliable, external oscillator, and no amount of fiddling with OSCCAL might help. But, it did in my case allow me to discover the value(s) which made SoftwareSerial tick at 8 MHz and a range of baud rates from 300 to 38400.
This allowed me to get the right value for the bit banging UART to work with the 1MHz clock which is not supported by SoftwareSerial.

AVR Butterfly - Timer reuse after internal oscillator calibration

I am calibrating the AVR Butterfly internal oscillator for being able to use USART, based on the sample code provided by AVR (see code below). As I also wanted to use two timer-controlled servo motors, I am wondering whether it is possible to reuse 16-bit timer 1 after the calibration process - I tried resetting the TCCR1A/B but it did not work out (code also below). I hope you can help me out with this.
void OSCCAL_Calibrate(void){
unsigned char calibrate = 0;
int temp;
unsigned char tempL;
CLKPR = (1<<CLKPCE); // set Clock Prescaler Change Enable
// set prescaler = 8, Inter RC 8Mhz / 8 = 1Mhz
CLKPR = (1<<CLKPS1) | (1<<CLKPS0);
TIMSK2 = 0; //disable OCIE2A and TOIE2
ASSR = (1<<AS2); //select asynchronous operation of timer2 (32,768kHz)
OCR2A = 200; // set timer2 compare value
TIMSK0 = 0; // delete any interrupt sources
TCCR1B = (1<<CS10); // start timer1 with no prescaling
TCCR2A = (1<<CS20); // start timer2 with no prescaling
while((ASSR & 0x01) | (ASSR & 0x04)); //wait for TCN2UB and TCR2UB to be cleared
delayMs(1000); // wait for external crystal to stabilise
while(!calibrate)
{
cli(); // disable global interrupt
TIFR1 = 0xFF; // delete TIFR1 flags
TIFR2 = 0xFF; // delete TIFR2 flags
TCNT1H = 0; // clear timer1 counter
TCNT1L = 0;
TCNT2 = 0; // clear timer2 counter
while ( !(TIFR2 & (1<<OCF2A)) ); // wait for timer2 compareflag
TCCR1B = 0; // stop timer1
sei(); // enable global interrupt
if ( (TIFR1 & (1<<TOV1)) )
{
temp = 0xFFFF; // if timer1 overflows, set the temp to 0xFFFF
}else
{ // read out the timer1 counter value
tempL = TCNT1L;
temp = TCNT1H;
temp = (temp << 8);
temp += tempL;
}
if (temp > 6250)
OSCCAL--; // the internRC oscillator runs to fast, decrease the OSCCAL
else if (temp < 6120)
OSCCAL++; // the internRC oscillator runs to slow, increase the OSCCAL
else
calibrate = 1; // the interRC is correct
TCCR1B = (1<<CS10); // start timer1
}
}
void motorInit(){
// reset timer 1
TCCR1A = 0;
TCCR1B = 0;
// initialize Servo Pins
DDRB |= (1<<PB5) | (1<<PB6);
ICR1H = ICR_VALUE >> 8;
ICR1L = ICR_VALUE & (TOP_VALUE);
// reset OCRs
setServoSpeed(0, 0);
setServoSpeed(1, 0);
// Set Timer mode (PWM Phase & Freq. correct, clear on compare match)
// and prescaler (8)
TCCR1A = ((1<<COM1A1) | (1<<COM1B1));
TCCR1B = ((1<<WGM13) | (0<<CS12) | (1<<CS11) | (0<<CS10));
}
Maybe you can check code which I used for a project while ago, but you should take care that I decreased the system frequency to 7.3768 MHz for 56700 baudrate, which maybe you need to adjust.
void OSCCAL_Calibrate(void)
{
uint8_t LoopCount = (0x7F / 2);
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
// Make sure all clock division is turned off (8MHz RC clock)
CLKPR = (1 << CLKPCE);
CLKPR = 0x00;
// Inital OSCCAL of half its maximum
OSCCAL = (0x7F / 2);
// Disable timer interrupts
TIMSK1 = 0;
TIMSK2 = 0;
// Set timer 2 to asyncronous mode (32.768KHz crystal)
ASSR = (1 << AS2);
// Ensure timer 1 control register A is cleared
TCCR1A = 0;
// Start both counters with no prescaling
TCCR1B = (1 << CS10);
TCCR2A = (1 << CS20);
// Wait until timer 2's external 32.768KHz crystal is stable
while (ASSR & ((1 << TCN2UB) | (1 << TCR2UB) | (1 << OCR2UB)));
// Clear the timer values
TCNT1 = 0;
TCNT2 = 0;
while (LoopCount--)
{
// Wait until timer 2 overflows
while (!(TIFR2 & (1 << TOV2)));
// Stop timer 1 so it can be read
TCCR1B = 0x00;
// Check timer value against ideal constant
if (TCNT1 > OSCCAL_TARGETCOUNT) // Clock is running too fast
OSCCAL--;
else if (TCNT1 < OSCCAL_TARGETCOUNT) // Clock is running too slow
OSCCAL++;
// Clear timer 2 overflow flag
TIFR2 |= (1 << TOV2);
Check this out!

Resources