explain this code [closed] - c

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
can anyone please explain this code?
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/signal.h>
char n = 0;
char FLAG =0x00;
char FLAG2 =0x00;
char RST=0x00;
unsigned char minutes_save [20];
unsigned char seconds_save [20];
int seconds, minutes, shift, count;
void init(void)
{
DDRB = 0xff;
DDRA =0xff;
MCUCR = 0x0F;
GICR = 0xC0;
TCCR2 = 0x05;
ASSR = 0x08;
TCNT2 = 0x00;
sei();
}
SIGNAL(SIG_INTERRUPT0)
{
if (FLAG == 0x00)
TIMSK = 0x40;
if (FLAG == 0x01)
TIMSK = 0x00;
FLAG = FLAG ^ 1;
}

Whenever the program receives an interrupt signal, it modifies the value of TIMSK to be 0x40 (64 in decimal) or 0x00 (0 in decimal) depending upon whether FLAG is currently set to be 0 or 1, and then it inverts the value of FLAG by performing a bitwise XOR operation with 1.
As for the rest of the code (the init() function, the other variables being declared, and the sei() function), there is not enough context provided by the code to determine what exactly it is doing/trying to do.
This page might be helpful: http://www.avr-asm-tutorial.net/avr_en/beginner/PDETAIL.html
It appears your code is setting register values on an ATMEL AVR embedded processor.

Related

Any clue as to why my code has errors in it? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I used MPLAB X IDE (a software for microcontrollers) to compile my code, but for some reason it keeps saying that there are at least two errors (specifically in the area that is bolded). I tried looking, but I'm still not sure why that is, so any help would be greatly appreciated.
#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
#include <math.h>
#include <p18f4620.h>
#pragma config OSC = INTIO67
#pragma config WDT = OFF
#pragma config LVP = OFF
#pragma config BOREN = OFF
#define delay 5
// Prototype Area to place all the references to the routines used in the program
void Init_ADC(void);
unsigned char Get_Full_ADC(void);
void Flash_LED(unsigned char);
void main(void)
{
unsigned int ADC_Result; // local variable to store the result
Init_ADC(); // initialize the A2D converter
TRISB =0x00; // make PORTB as all outputs
while(1)
{
ADC_Result = Get_Full_ADC(); // call routine to measure the A2D port
Flash_LED(ADC_Result); // call routine to flash the LED based on the delay
// indicated by ADC_Result
}
}
void Init_ADC(void)
{
ADCON0=0x01; // select channel AN0, and turn on the A2D subsystem
ADCON1=0x0E; // set pin 2 as analog signal, VDD-VSS as reference voltage
// and right justify the result
ADCON2=0xA9; // Set the bit conversion time (TAD) and acquisition time
}
**unsigned int Get_Full_ADC(void)
{
int result;
ADCON0bits.GO=1; // Start Conversion
while(ADCON0bits.DONE==1); // Wait for conversion to be completed (DONE=0)
result = (ADRESH * 0x100) + ADRESL; // Combine result of upper byte and lower byte into
return result; // return the most significant 8- bits of the result.
}**
void Flash_LED(unsigned int ADC_result)
{
unsigned int counter1, counter2;
LATB = 0x0A; // output to PORTB the pattern 00001010
// delay loop
for (counter2=delay; counter2>0; --counter2)
{
for (counter1=ADC_result ; counter1>0; -- counter1);
}
LATB = 0x05 // output to PORTB the pattern 00000101
// delay loop
for (counter2=delay; counter2>0; --counter2)
{
for (counter1=ADC_result ; counter1>0; -- counter1);
}
}
The function prototype (declaration) says
unsigned char Get_Full_ADC(void);
but its definition says
unsigned int Get_Full_ADC(void)
and also you have
int result;
...
return result;
So you never use the consistent type. The compiler will complain about the non-matching definition.

I need to know why my interrupts are not working here

I am using stm8l - discovery and i have created a code for toggling a led for every 1 second using timer (TIM1) but this is not working properly. I am missing something here in my configuration
I could enter the interrupt function for the first time but after that it does not enter the interrupt function. Someone please look into and help me out
enter code here
#include <iostm8l.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "defs.h"
unsigned int count = 0;
#svlreg #interrupt void TIM1(void)
{
count += 1;
TIM1_SR1 &= ~(0x01);
}
main()
{
CLK_DIVR = 0x00; // Set the frequency to 16Mhz
CLK_PCKENR2 = 0x02; // clock for timer1
PC_DDR = 0x80; // direction output for led
PC_CR1 = 0x80; // fast push pull mode
PE_DDR = 0x80; // direction output for led
PE_CR1 = 0x80; // fast push pull mode
TIM1_PSCRH = 0x3e; //to create a frequency for 1000 hz
TIM1_PSCRL = 0x80; // so prescalar is 16000
TIM1_CR1 = 0x01;
TIM1_IER = 0x01;
_asm("rim\n");
while(1)
{
if (count == 1000)
{
PE_ODR ^= 0x80;
count = 0;
}
}
}
The interrupt enters only one time but after that it does not enter. So variable "count" remains at value 1
You are using magic numbers for the bit masks instead of defined constants, so the code is pretty damn hard to read for you and me both. Change this so that the code ends up like for example
TIM1_SR1 &= ~TIM1_SR1_UIF;
Since this is a 8 bit MCU it is also absolutely essential that you u suffix all integer contants, or they will be of type signed int, which you don't want.
For example this code TIM1_SR1 &= ~(0x01); is equivalent to TIM1_SR1 &= -2. Very easy to write accidental subtle bugs this way. I recommend studying Implicit type promotion rules.
It is highly recommended to disassemble every ISR you write to see what machine code you actually end up with, and single step it through the debugger while watching the register as well. This particular register seems to ignore having 1 written to it, so you could probably just do TIM1_SR = TIM1_SR1_UIF;. Incorrectly cleared timer flags inside ISRs is one of the most common bugs in embedded systems.
Quoting the manual:
UIF: Update interrupt flag
– At overflow or underflow if UDIS = 0 in the TIM1_CR1 register
– When CNT is re-initialized by software using the UG bit in TIM1_EGR register, if URS = 0 and UDIS = 0 in the TIM1_CR1 register.
– When CNT is re-initialized by a trigger event (refer to the TIM1_SMCR register description), if URS = 0 and UDIS = 0 in the TIM1_CR1 register
Your code doesn't appear to do any of this, so it is pretty safe to assume the timer counter isn't reset.
Another problem is that count must be declared as volatile or otherwise the compiler might optimize out this code completely:
if (count == 1000)
{
PE_ODR ^= 0x80;
count = 0;
}

Ultrasonic sensor interfacing with AVR [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm working on a project part of it to have some reads with ultrasonic sensor and send it with serial communication, i wrote code and it gives random reads and sometimes gives 0 as a read, is the formula i used for finding distance right !?, or there is another formula, I'm using Atmega32 with internal 8MHz clock, can someone help me and know what's wrong with my code !?.
#define F_CPU 8000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
void inti_serial();
static volatile int pulse = 0;
static volatile int change = 0;
int main(void)
{
/* Replace with your application code */
inti_serial();
MCUCR |= (1 << ISC00); //Any logical change on INT0
GICR |= (1 << INT0); //Enable INT0
TCCR1A=0;
sei();
while (1)
{
PORTC |= (1<<0);
_delay_us(15);
PORTC &= ~(1<<0);
while(!(UCSRA & (1<<UDRE)));
UDR = ((pulse/2)*1*(1/F_CPU)*343) ;
_delay_ms(100);
}
}
ISR(INT0_vect){
if (change==1)//when logic from HIGH to LOW
{
TCCR1B=0;//disabling counter
pulse=TCNT1;//count memory is updated to integer
TCNT1=0;//resetting the counter memory
change=0;
}
if (change==0)//when logic change from LOW to HIGH
{
TCCR1B|=(1<<CS10);//enabling counter
change=1;
}
}
void inti_serial()
{
UCSRB |= (1<<TXEN);
UCSRC |= (1<<UCSZ0) | (1<<UCSZ1) | (1<<URSEL);
UBRRL = 0x33;
}
I see a few options for improvement in your development:
You are writing a sample from the ISR to variable and you read it continuously from the main loop and output it. Instead you should only output a new sample once (makes the serial data much smaller and easier to concentrate on the actual content and sample timing)
Before you think about the correct formula you should verify that your sampling mechanism is right. Without details about your sensor, nobody here can judge your formula, anyway.
Instead of sampling a free running counter you could use the input capture circuit of the processor (more accurate, less jitter due to interrupt latency)
Instead of resetting the counter register to zero you could subtract two consecutive samples from each other (less sample jitter due to interrupt latency)
Instead of deducing the edge from a toggled flag, ask the hardware about the state of the pin or which edge triggered the interrupt (or the capture)

C8051f312 microcontroller [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I'm not very good at C language, but I have write a very simple code to a C8051F312 microcontroller.
My code doesn't working. Please help me what did I wrong.
#include C8051F310.h
#include stdio.h
sbit LED_16 = P1^7; // green LED: 1 = ON; 0 = OFF
void init(void)
{
// XBRN registers_init
XBR0 = 0x00;
XBR1 = 0x00; // Enable the crossbar
PCA0MD = 0X00;
// port_init
P0MDOUT = 0x00; // Output configuration for P0
P1MDOUT = 0x40; // Output configuration for P1
P2MDOUT = 0x00; // Output configuration for P2
P3MDOUT = 0x00; // Output configuration for P3
}
void main(void)
{
init();
while (1)
{
LED_16 = 1; // LED continuously illuminated
}
}
1.First of all you should use one of 2 following options for #include directive
#include "path-spec"
#include <path-spec>
, not #include path-spec, as you did
2.To configuire 7th bit of P1 general I/O port to work in push-pull mode you should set
P1MDOUT = 0x80;
, not
P1MDOUT = 0x40;

Serial Monitor Input for the Arduino? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
This is just a simple test program. I'm trying to have the Arduino print "received" on an LCD screen attached to it. I think it's my if statement causing the error, any ideas?
Currently when "send" is put in the serial monitor nothing happens.
Here is the code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
char serialinput; // for incoming serial data
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
serialinput = Serial.read();
if (serialinput == 'send')
{
lcd.print("received");
}
}
}
You're reading a byte (a char in C) from your serial port, but you try to compare it to a string:
If you want to read 4 char and compare it to "send" then you would have to do something like:
#include <LiquidCrystal.h>
#include <string.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
char serialinput [5] = {0}; // for incoming serial data
// 4 char + ending null char
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
memmove (serialinput, &serialinput[1], 3); // Move 3 previous char in the buffer
serialinput [3] = Serial.read(); // read char at the end of input buffer
if (0 == strcmp(serialinput, "send")) // Compare buffer content to "send"
{
lcd.print("received");
}
}
}
Assuming that <string.h> header is valid within the Arduino SDK
PS : litteral strings in C code are written between " (double quotation marks). ' is for characters.
What errors did you have when uploading to the arduino ?

Resources