I'm using C to write a program on an 8051 microcontroller. The compiler I'm using is Keil Microvision. I'm stuck and having trouble figuring out what is missing from my code. I know it's very basic code I just can't figure out what I'm supposed to do.
So pretty much what I am doing is taking sending a sentence out to the user and having them answer yes or no through the serial port and I used a serial interrupt. That part works fine. If I get a no from the person I want to generate a square wave 5kHz by a timer interrupt. I want this square wave to be controlled by an external interrupt turning it on and off when the external interrupt on pin P3.2 is either on or off.
Here is all my code
#include <REG52.H>
#include <stdio.h>
sbit WAVE = P1 ^ 7;
#define BIT(x) (1 << (x))
void timer0() interrupt 1 // timer is controlling square wave timer 0
{
WAVE = ~WAVE;
}
void interrupt0() interrupt 0
{
IE ^= BIT(1);
}
void serial0() interrupt 4
{
unsigned char x;
unsigned int i, z;
unsigned char yes[] = " YES ";
unsigned char no[] = " NO ";
unsigned char nvalid[] = " NOT VALID TRY AGAIN ";
while (RI == 1) {
x = SBUF;
RI = 0;
if (z < 1) {
if (x == 'n') {
for (i = 0; i < 4; i++) {
SBUF = no[i];
while (TI == 0) ; //wait for transmit
TI = 0;
z++;
}
}
} else {
return;
}
if (x == 'y') {
for (i = 0; i < 5; i++) {
SBUF = yes[i];
while (TI == 0) ;
TI = 0;
}
} else if (x != 'n') {
for (i = 0; i < 21; i++) {
SBUF = nvalid[i];
while (TI == 0) ;
TI = 0;
}
}
TI = 0;
return;
}
}
void main()
{
TMOD = 0x20;
TH1 = 0xF6; //baud rate
SCON = 0x50;
TH0 = 0xA4;
IE = 0x93; //enable interrupts
IP = 0x10; // propriety to serial interrupt
TR1 = 1; //start timer 1
TR0 = 1; //clear timer 0
TI = 1;
printf("Hello, Are you okay? Press y for yes and n for no ");
while (1) ;
}
The part I'm having trouble with is these two interrupt from the previous code
void timer0() interrupt 1 // timer is controlling square wave timer 0
{
WAVE=~WAVE;
}
void interrupt0() interrupt 0
{
IE ^= BIT(1);
}
Any hints in the right direction would be greatly appreciated! Thanks. Sorry about formatting
Variables that are modified by interrupts should be defined as volatile:
volatile sbit WAVE = P1 ^ 7;
Related
I am new to programming in IDE. I am using built-in timer of arduino.I am using TDR method to capture reflection from fault location in cable and for that I am using timer.Timer should start as soon as output is send and stop at reflection. I have a reference code for it but I dont able to understand it, so if anyone know about it , it would be great.
void setup()
{
pinMode(stepPin, OUTPUT);
pinMode(refPin, OUTPUT);
pinMode(shutdownPin, OUTPUT);
TCCR1A = 0;
TCCR1B = (1 << ICNC1); // input capture noise canceller enabled, capture on falling edge
TIMSK1 = 0; // timer 1 interrupts disabled
ACSR = 0; // input capture from ICP1 pin
TCCR2B = (1 << CS20); // change timer 2 PWM frequency to 31.25kHz because we're using pin 11 as a DAC
Serial.begin(19200);
}
struct Step
{
unsigned int time;
unsigned int amplitude;
};
// Take a single measurement, using either a positive or negative edge from the comparator.
// The comparator reference voltage must have been set up and allowed to stablise before calling this.
unsigned int takeMeasurement(bool posEdge)
{
byte reg1b = (posEdge) ? 0 : (1 << ICES1); // input capture noise canceller csdisabled, set up input capture polarity, stop timer
reg1b |= (1 << CS10);
TCCR1B = reg1b;
TCNT1H = 0;
TCNT1L = 0; // clear timer
unsigned int capture = 0;
unsigned long start = micros(); // get the time
cli();
TCNT1H = 0;
TCNT1L = 0; // clear timer
TIFR1 = (1 << ICF1); // clear timer 1 input capture bit
PORTD |= (1 << 4); // set output high
sei();
do
{
if ((TIFR1 & (1 << ICF1)) && capture == 0)
{
byte temp = ICR1L;
capture = (ICR1H << 8) | temp;
}
} while (micros() - start < 100);
PORTD &= ~(1 << 4); // set output low
return capture;
}
size_t findSteps(bool positive, struct Step *results, size_t maxResults)
{
byte amplitude = (positive) ? 5 : 250;
analogWrite(refPin, amplitude);
delay(100); // wait 100ms for the output to stabilise
unsigned int lastReading = 0;
size_t numResults = 0;
unsigned int stepSize = 0; // 0 means not in a step
#ifdef DEBUG
Serial.print((positive) ? "pos " : "neg ");
#endif
for (int i = 0; i < 50; ++i)
{
analogWrite(refPin, amplitude);
delay(10);
unsigned int currentReading = takeMeasurement(positive);
unsigned int currentDiff = currentReading - lastReading; // diff since start of possible step
if (stepSize == 0)
{
// Not currently in a step
if (i != 0 && currentReading != 0 && currentDiff == 0)
{
// Found the start of a possible step
++stepSize;
}
lastReading = currentReading;
}
else
{
if (currentDiff > 2 || i + 1 == 50)
{
// Step has endeed, so record it if it is big enough
if (stepSize >= 2)
{
results->time = lastReading;
results->amplitude = amplitude - 5;
++results;
++numResults;
if (numResults == maxResults) break;
}
stepSize = 0;
lastReading = currentReading;
}
else if (currentDiff == 0)
{
++stepSize;
}
}
#ifdef DEBUG
if (i != 0) Serial.write(',');
Serial.print(currentReading);
#endif
if (positive)
{
amplitude += 5;
}
else
{
amplitude -= 5;
}
}
#ifdef DEBUG
Serial.println();
#endif
return numResults;
}
I am interfacing LM35 with Atmega8. To display digits I use 7 segment LED anode display that I connect to AVR both ends (it handles it without transistors so why not). Strange thing happens:
res value after assigning it from adc is 237 (23.7 degrees). I want to print on my display the first digit (2).
If I leave last line in the while commented out, the display first shows digit 2 correctly but after the first delay it shows 1 instead of 2. Otherwise I get correctly digit 2. Why is this happening?
#ifndef F_CPU
#define F_CPU 1000000UL
#endif // F_CPU
#include <avr/io.h>
#include <util/delay.h>
#define DELAY_IN_MS 500 /* 0.5 sec */
int numbers[] = {
0b01000000,
0b01110011,
0b00100100,
0b00100001,
0b00010011,
0b00001001,
0b00001000,
0b01100011,
0b00000000,
0b00000001,
0b11111111 // off
};
uint8_t digits[3];
void initADC()
{
ADMUX=(1<<REFS1)|(1<<REFS0);
ADCSRA=(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
}
uint16_t ReadADC(uint8_t ch)
{
//Select ADC Channel ch must be 0-7
ch=ch&0b00000111;
ADMUX|=ch;
//Start Single conversion
ADCSRA|=(1<<ADSC);
//Wait for conversion to complete
while(!(ADCSRA & (1<<ADIF)));
//Clear ADIF by writing one to it
ADCSRA|=(1<<ADIF);
return(ADC);
}
int main()
{
DDRD = 0xFF;
PORTD = 0xFF;
DDRB = 0b00000001;
PORTB = 1;
initADC();
uint16_t adc_value;
uint16_t res;
while(1)
{
adc_value = 0;
for (int i = 0; i < 250; i++)
{
adc_value += ReadADC(0);
}
adc_value=(adc_value/25)/4;
res = adc_value;
for(int j = 2; j >= 0; j--) {
digits[j] = res%10;
res /= 10;
}
uint8_t dig = digits[0];
PORTD = numbers[dig];
_delay_ms(DELAY_IN_MS);
// if following is uncommented there blinks digit two correctly
// if commented there is unblinking digit 1
PORTD = numbers[10]; // display off
}
return 0;
}
The problem was induction.
My circuit had many wires in non-soldering-field. When the display was on, there was a lot of induction going on changing resulting voltage on ADC input/LM35 output.
There is more than one solution.
1) Software: I moved ADC conversion into the interruption function. It turns of the displays, converts value from lm35 and displays digit on proper display. It happens so fast that the eye cant perceive it.
I prefer this one for now, because it makes my circuit simpler.
2) Hardware: adding L/C or R/C filter to adc pin should resolve the issue.
Full code for 1)
#ifndef F_CPU
#define F_CPU 1000000UL
#endif // F_CPU
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#define DELAY_IN_MS 5000 /* ms */
#define NUM_OF_MEASUREMENTS 100
#define NUM_DISPLAYS 3
int numbers[] = {
0b10000001,
0b10011111,
0b10100100,
0b10010100,
0b10011010,
0b11010000,
0b11000000,
0b10011101,
0b10000000,
0b10010000,
0b11111111 // off
};
int display = 0;
uint8_t digits[NUM_DISPLAYS];
volatile uint16_t adc_values[NUM_OF_MEASUREMENTS];
int adc_read_cycle_index = 0;
uint32_t res;
void initADC()
{
ADMUX=(1<<REFS1)|(1<<REFS0);
ADCSRA=(1<<ADEN)|(1<<ADPS2);
}
uint16_t ReadADC(uint8_t ch)
{
//Select ADC Channel ch must be 0-7
ch=ch&0b00000111;
ADMUX|=ch;
//Start Single conversion
ADCSRA|=(1<<ADSC);
//Wait for conversion to complete
while (ADCSRA & (1<<ADSC));
return(ADC);
}
void readDegrees()
{
adc_values[adc_read_cycle_index] = (ReadADC(0)*10)/4;
if(adc_read_cycle_index + 1 == NUM_OF_MEASUREMENTS) {
adc_read_cycle_index = 0;
} else {
adc_read_cycle_index++;
}
}
void fetchTemperatureDigits() {
res = 0;
for(int i = 0; i < NUM_OF_MEASUREMENTS; i++) {
res += adc_values[i];
}
res /= NUM_OF_MEASUREMENTS;
for(int j = 2; j >= 0; j--) {
digits[j] = res%10;
res = res / 10;
}
}
void initTimer0()
{
// Prescaler = FCPU/64
TCCR0|=(1<<CS01);//|(1<<CS00);
//Enable Overflow Interrupt Enable
TIMSK|=(1<<TOIE0);
//Initialize Counter
TCNT0=0;
}
ISR(TIMER0_OVF_vect)
{
// turn off displays
PORTD = numbers[10];
// read ADC and convert to degrees
readDegrees();
// turn on proper anode
PORTB &= 0b11111000;
PORTB |= (1<<display);
// show digit
PORTD = numbers[digits[display]];
// show decimal point for second display (21.5 - second display shows "1.")
if(display == 1) {
PORTD &= 0b01111111;
}
// next display for next interruption
display++;
if(display == NUM_DISPLAYS) {
display = 0;
}
}
int main()
{
initADC();
for(int i = 0; i < NUM_OF_MEASUREMENTS; i++) {
readDegrees();
}
DDRD = 0xFF;
PORTD = 0;
DDRB |= 0b00000111;
PORTB |= 1;
initTimer0();
sei();
while(1) {
fetchTemperatureDigits();
_delay_ms(DELAY_IN_MS);
}
return 0;
}
I'm programming my C code onto a PIC board. My question is how to get my program to count how long a button (RB0) is pressed down. It then displays the time it took and display it on an LCD Display. It counts in ms. Below is my code so far.
// Global Variables
unsigned int COUNTER;
// Subroutine Declarations
#include <pic18.h>
#include "lcd_portd.c"
//LCD routine, modified from previous examples
void LCD_Out(unsigned int STUFF, unsigned char A)
{
unsigned char C[5], i;
for (i=0; i<5; i++)
{
C[i] = STUFF % 10;
STUFF = STUFF / 10;
}
for (i=5; i>0; i--)
{
if (i == A) LCD_Write('.');
LCD_Write(C[i-1] + '0');
}
}
void interrupt IntServe(void)
{
if (TMR2IF)
{
RB0 = !RB0;
TMR2IF = 0;
COUNTER = COUNTER + 1;
}
}
// Main Routine
void main(void)
{
//Instantiate all Ports to ready for Timers and pin sets
LCD_Init();
TRISA = 0;
TRISB = 0;
TRISC = 0;
TRISD = 0;
TRISE = 0;
ADCON1 = 0x0F;
//Timer Interrupt for 1 ms, A = 9, C = 4, B = 250
//Which means PR2 = 249, and # is x1001101 = 0x4D
T2CON = 0x4D;
PR2 = 249;
TMR2IE = 1;
PEIE = 1;
TMR2ON = 1;
TMR2IP = 1;
GIE = 1;
// While Loop displays length of Wait through Counter
while(1)
{
LCD_Move(0,0);
LCD_Out(COUNTER,3);
}
}
you need figure out how your button is connected to your microcontroller and declare that port in you main function. Make sure you are able to read the state of the port (i.e. button up or button down).
Then it your IntServe interrupt check if the button is down, if it is increment COUNTER.
The COUNTER is an integer, so it will only be able to count upto 65,535 ms (about 65 seconds), you may want to implement a second count variable for overflow, so that you can count button presses longer than 65 seconds.
I have this code of a transmitter and I have a problem where when I get input from my transmitter I save it in a char array "rec" and after I have finished receiving I compare it with my other arrays and base on that comparison I light up a bulb in portb. But the code gives me no errors and no bulb lights (the connection works I've tried it) it has something to do with the comparing arrays part. Am I doing this right? Thank you
char rec[3];
char cmp1[]= "1000";
char cmp2[]= "1010";
char cmp3[]="1111";
char cmp4[]="1001";
int i=0;
int beginrecord;
void main(void)
{
TRISB=0;
TRISD=0;
TRISC=255;
PORTC=0;
PORTB=0;
while(1)
{
if(PORTC==1)
{
rec[i]=1;
i++;
beginrecord = 1;
delay_ms(1);
}
if (PORTC==0 && beginrecord==1)
{
rec[i]=0;
i++;
delay_ms(1);
}
if(i==4) {
beginrecord = 0;
i = 0;
if(rec == cmp1){
portb = 0x01;}
else if(rec == cmp2){
portb = 0x02;}
else if(rec == cmp3){
portb = 0x04;}
else if(rec == cmp4){
portb = 0x08;}
}
}
}
You must use strcmp(rec, cmp1) == 0 instead of rec == cmp1.
The way you do it now, you compare only the pointers, which are never the same.
And are you sure that your port returns a character 1or 0 and not the values 0/1?
In that case your comparison also wouldn't work.
Some other issues with your code:
I assume that the char form the port is a binary value and not an ASCII character.
If it is indeed an ASCII character and you use strcmp() then you must add a zero byte at the end of the buffer.
If you receive multiple bytes and not bits, you must use memcmp() instead of strcmp(). In that case your intialisation of cmpX would be also wrong. If it are bits you receive, you would want to combine them into bytes before comparing (assuming a serial port).
PORTC is always 0, so it can not become 1 in your code.
You seem to have mixed case PORTB vs. portb.
your variables have no type.
Here you are declare three cmp[] of char type which is a string so it is like "1000". it is 4 byte data. and your rec[3] is only 3 byte. i think you try to compare a int type. like
1001 == 1001 in binary
Declare them as unsigned int then try to compare.
And make all your values full 8 bit like 00001000b not 1000.
or may be i get it wrong.
There are a number of issues that confuse your goal - see above comment, but I've tried to address them below.
Rather than accumulate the rec as an array, the bits are accumulated in an int. This simplifies the compares to your original style.
beginrecord needed initialization. Variables were brought into a local scope.
Cleaned up the indentation.
const int cmp1 = 8; // "1000";
const int cmp2 = 10; // "1010";
const int cmp3 = 15; // "1111";
const int cmp4 = 9; // "1001";
void main(void) {
TRISB = 0;
TRISD = 0;
TRISC = 255;
PORTC = 0;
PORTB = 0;
int i = 0;
int beginrecord = 0;
int rec = 0;
while (1) {
if (PORTC == 1) {
rec <<= 1; /* rec = rec * 2 */
rec |= 1;
beginrecord = 1;
delay_ms(1);
i++;
}
else if (PORTC == 0 && beginrecord == 1) {
rec <<= 1;
delay_ms(1);
i++;
}
if (i == 4) {
if (rec == cmp1) {
PORTB = 0x01;
} else if (rec == cmp2) {
PORTB = 0x02;
} else if (rec == cmp3) {
PORTB = 0x04;
} else if (rec == cmp4) {
PORTB = 0x08;
}
beginrecord = 0;
i = 0;
rec = 0;
}
}
}
So, I have to do this challenge, which is to implement a camera surveillance system for a 8051 microcontroller.
These are the specifications:
Each camera is related to a movement sensor, and each time it detects a movement, the recording of this camera will be among the ones that will be registered and saved. If the sensor doesn't capture any movement for more than 5 seconds, this camera will not be recorded anymore;
If there's no camera on, the video recorder must be on "pause";
If more than one camera is on, a multiplexer (mux) have to be used to select the camera signals in a way so each camera is recorded during 3 seconds. This way, all the active cameras must be recorded during 3 seconds. If just one camera is active, it's signal must be the only one in the mux.
This I have already accomplished in the code below. And what we have to do now is to optimize the size of the code without the compiler optimizations. The code is 198 bytes by now, but I'm trying to get below 180 bytes.
Is it possible? I already tried to do the calculations of the #define, but the compiler already optimize that for me.
#include <REG51F.h>
#define TIMEOUT 50
#define TIMEOUT_REC 30
#define FrClk 12000000
#define FreqTimer0_emHz 10
#define VALOR_TH0 ((65536 - (FrClk /(12 * FreqTimer0_emHz ))) >> 8)
#define VALOR_TL0 ((65536 - (FrClk /(12 * FreqTimer0_emHz ))) & 0xFF)
data bit PAUSE_INT;
data bit PAUSE_TMP;
sbit PAUSE = P0^0;
sbit SENSOR1 = P0^1;
sbit SENSOR2 = P0^2;
sbit SENSOR3 = P0^3;
sbit SENSOR4 = P0^4;
sbit MUX0 = P0^5;
sbit MUX1 = P0^6;
data unsigned char CAM[4];
data unsigned char REC;
data unsigned char index;
data unsigned char count;
void timer0_int (void) interrupt 1 using 2 {
for (index = 0; index < 4; index++)
if(CAM[index])
CAM[index]--;
if (!PAUSE_INT && REC)
REC--;
else
{
REC = TIMEOUT_REC;
index = (index + 1) & 0x03;
for (count = 0; !CAM[index] && count < 4; index = (index + 1) & 0x03, count++);
MUX0 = index & 0x1;
MUX1 = index & 0x2;
PAUSE_INT = 0;
}
}
int main(void)
{
PAUSE_TMP = 1;
PAUSE_INT = 0;
index = 0;
//timer0_init
EA = 1;
TR0 = 0;
TMOD = (TMOD & 0xF0) | 0x01;
TH0 = VALOR_TH0;
TL0 = VALOR_TL0;
ET0 = 1;
TR0 = 1;
while(1) {
if (SENSOR1)
{
CAM[0] = TIMEOUT;
}
if (SENSOR2)
{
CAM[1] = TIMEOUT;
}
if (SENSOR3)
{
CAM[2] = TIMEOUT;
}
if (SENSOR4)
{
CAM[3] = TIMEOUT;
}
if (CAM[0] || CAM[1] || CAM[2] || CAM[3])
{
if (PAUSE_TMP)
PAUSE_INT = 1;
PAUSE_TMP = 0;
}
else {
PAUSE_TMP = 1;
}
PAUSE = PAUSE_TMP;
}
}
You're probably going to have to look at the generated assembly code for this in order to wring the last few bytes out of it. It's probably possible to shave a few here and there by reusing a variable or combining operations. The resulting code won't be pretty - or maintainable - but it just might get you below your cutoff.
I think a switch case instead of if(sensor1,2,3,4) could help some.