Using IDE : AtmelStudio, uC = atmega328p, sensor DHT11
Im trying to read the values from DHT11.
I have set the start signal
DDRC |= 1 << TEMP_SENZOR; //output
_delay_ms(2000); // wait for 2s according to datasheet
PORTC &= ~ (1 << TEMP_SENZOR); //set low for at least 18 ms
_delay_ms(20);
PORTC |= 1 << TEMP_SENZOR; // set high
Then i have set getResponse metod
DDRC &= ~ (1 << TEMP_SENZOR); // set for input
while(PINC & (1 << TEMP_SENZOR));
while(PINC & (1 << TEMP_SENZOR) == 0);
while(PINC & (1 << TEMP_SENZOR));
I have tried to code function several times, to get values but unfortunately i get some values that have nothing to do with reading from Proteus DHT11.
Im new to this, so sorry if question is trivial.
Related
I have the assignment to create a running light on my Arduino with the following requirements/constraints
Requirements
Implement a socalled “runninglight” with 3 leds.
The on time of each led is the same, being 0.5 sec.
After the last Led is lit, the first starts again.
Constraints
The leds are connected to pin 8,9 and 10 of the Arduino. (don’t forget the resistors)
The running led must be implemented in the loop of the Arduino
The setup() function may contain any Arduino function/macro.
Only the Arduino function delay() is allowed in the loop.
No use of any other Arduino function/macro etc. in the loop.
No use of for/while in the loop() function
No use of your own defined functions
Your loop function must contain not more then 10 statements.
So in the loop you may only use
o Delay function
o Your variables
o The following characters/digits: * ( ) & ~ | ; = < > ! + - 0 1 2 3 4 5 6 7 8 9
I have tried the following code
#include <Arduino.h>
//mask for setting correct bits in register to setup leds to 1
#define DDBMASK 0x07
//masks for setting correct bits in register for led control to 1
#define LEDMASK 0x01
byte *ddBPtr; //point to ddB register (data direction)
byte *portBPtr; //point to portB resiger (data register)
void setup() {
//setup data direction register to set pins to output
ddBPtr = (byte *) 0x24;
*ddBPtr |= DDBMASK; // 0b0000 0111
//assign pointer to right register
portBPtr = (byte *) 0x25;
}
void loop() {
//use data register (portB) for controlling leds
*portBPtr ^= LEDMASK;
delay(500);
*portBPtr ^= LEDMASK;
*portBPtr ^= LEDMASK << 1;
delay(500);
*portBPtr ^= LEDMASK << 1;
*portBPtr ^= LEDMASK << 2;
delay(500);
*portBPtr ^= LEDMASK << 2;
}
Apparently this is possible with only one delay function and following the requirements and constrains. I have been stuck on this for hours and cant figure it out.
I tried the following which also does not work because I'm unable to reset my counter back to zero
void loop() {
//use data register (portB) for controlling leds
*portBPtr ^= (LEDMASK << ledCount);
delay(500);
*portBPtr ^= (LEDMASK << ledCount);
ledCount++;
//cant figure out a way to reset ledCount back to 0 after 3 itterations
//running light only runs once so first led does not light after third led is lit
}
What am I missing?
Perhaps this:
*portBPtr |= (1 << ledCount);
delay(500);
*portBPtr &= ~(1 << ledCount);
ledCount = (9 >> (ledCount << 1)) & 3;
This also avoids ^ which is not in your list of allowed symbols.
The line ledCount = (9 >> (ledCount << 1)) & 3 is like a lookup table with each entry taking up two bits. It maps 0 to 1, 1 to 2, 2 to 0. The normal way to write this code is ledCount = (1 + ledCount) % 3;
This might also work, being a little simpler:
delay(500);
*portBPtr = (1 << ledCount);
ledCount = (9 >> (ledCount << 1)) & 3;
By using assignment rather than | or & or ^, all the other bits will be zero, presumably causing the non-current leds to turn off.
I am currently in the process of building a drum machine with a micro controller. I am trying to figure out the logic of the sequencer. I have 16 LED's which will indicate at which 16th note the drum machine currently is playing.
So for instance, let's say the beats per minute (BPM) is 120, then the led should shift twice every second.
So for the shifting part I have written code such that if the step number is 0, we shift in a 1. If the step number is > 0, we shift in a 0. The method is called every (60/BPM) seconds.
PD5 is the serial input,
PD4 is the latch pin, PD3 is the clock pin.
void update_led(void) {
if (step_number == 0){
PORTD |= (1 << PD5); //Send a 1
PORTD |= (1 << PD4); //Read the input to 1st led
PORTD &= ~(1 << PD5);
PORTD &= ~(1 << PD4);
PORTD |= (1 << PD3); //Shift to next led
_delay_ms(40); //Apparently I need a delay here to light up LED
PORTD &= ~(1 << PD3);
}else{
PORTD |= (1 << PD4);
PORTD &= ~(1 << PD4);
PORTD |= (1 << PD3);
_delay_ms(40);
PORTD &= ~(1 << PD3);
}
}
But I also want the LEDs to static be lit for each step a user has programmed a sound. So for instance if the user presses button 1 and 5 and 9 and 13 with the instrument kick drum, the LED 1,5,9,13 should be static lit up but the leds should also shift like the above code. Does anyone have any tips on how to efficiently implement this? Is there a smart way to make a specific LED always light up when shifting the other LED's?
I am trying to design a 4-bit adder. I've set my input and output ports but am not sure where to go from there.
My "B" input port is Port D, my "A" input port as well as my "Cin" is Port B, and my "S" output port as well as my "Cout" is Port C. I am having trouble figuring out how to isolate the individual ports (such as the carry ripple) and am pretty much out of ideas aside from nested if-statements.
My code is currently as follows:
#include <avr/io.h>//library used to access the pin addresses
int main () {
DDRD &= ~(0b00111100);//B inputs
DDRB &= ~(0b00011111);//Carry-in + A inputs
DDRC |= 0b00011111;//Carry-out + S outputs
while (1) {
//PORTC |= PIND + PINB;
//PORTC &= ~(PIND + PINB);
if ((PIND & 0b00000000)&&(PINB & 0b00000000)) {
PORTC |= 0b00000000;
PORTC &= ~(0b00000000);
}
else if ((((PIND & 0b00000100)||(PIND & 0b00001000)||(PIND & 0b00010000)||(PIND & 0b00100000))&&(PINB & 0b00000000))||(((PINB & 0b00000100)||(PINB & 0b00001000)||(PINB & 0b00010000)||(PINB & 0b00100000))&&(PIND & 0b00000000)))
PORTC |= 0b00000001;
PORTC &= ~(0b11111110);
}
}
return 0;
}
If you want to refer to a specific pin/pins then it's relatively easy (assuming the registers are set as outputs):
For example, to isolate pin 4 on PORTD you do:
PORTD |= (1 << PIND4);
This will set pin 4 in PORTD to HIGH.
PORTD |= (1 << PIND4) | (1 << PIND5);
This will set pins 4 and 5 in PORTD to HIGH.
PORTD &= ~(1 << PIND4);
This will set pin 4 in PORTD to LOW.
PORTD &= ~(1 << PIND4) & ~(1 << (PIND5);
This will set pins 4 and 5 in PORTD to LOW.
You can also define a macro for the (1 << n) logic:
#define _BV(n) (1 << (n))
These tutorial have it all explained pretty well: http://maxembedded.com/2011/06/port-operations-in-avr/ and https://efundies.com/avr-bitwise-operations-in-c/.
If you go through the bitwise logic step by step on a sheet of paper it will become clearer!
I am trying to generate a 16kHz pwm... this is the code i am working with right now. `
int main(void){
DDRD |= (1 << DDD6);
// PD6 is now an output
OCR0A = 128;
// set PWM for 50% duty cycle
TCCR0A |= (1 << COM0A1);
// set none-inverting mode
TCCR0A |= (1 << WGM01) | (1 << WGM00);
// set fast PWM Mode
TCCR0B |= (1 << CS01);
// set prescaler to 8 and starts PWM
while (1);
{
// we have a working Fast PWM
}}`
The default frequency is set to 64kHz... is there any way i can change the default frequency ? Because changing the prescalars does not help me get a frequency of 16kHz...
If you can use CTC mode instead of fast PWM (which means, you don't need to vary the pulse-pause-width ratio), you can use CTC mode to accomplish that:
DDRD |= (1 << DDD6);
// PD6 is now an output
OCR0A = 32;
// set PWM for 12.5% duty cycle
TCCR0A |= (1 << COM0A0);
// set toggle mode
TCCR0A |= (1 << WGM01);
// set CTC Mode
TCCR0B |= (1 << CS01);
// set prescaler to 8 and start PWM
I'm tinkering around with an ATMega328P right now and wanted to read an analogue value from a pin through the ADC and simply output the value to 4 LEDs. Really simple
#define F_CPU 20000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#define BRIGHTNESS_PIN 2
#define ADC_SAMPLES 5
void init_adc()
{
//set ADC VRef to AVCC
ADMUX |= (1 << REFS0);
//enable ADC and set pre-scaler to 128
ADCSRA = (1 << ADPS0) | (1 << ADPS1) | (1 << ADPS2) | (1 << ADEN);
}
uint16_t read_adc(unsigned char channel)
{
//clear lower 4 bits of ADMUX and select ADC channel according to argument
ADMUX &= (0xF0);
ADMUX |= (channel & 0x0F); //set channel, limit channel selection to lower 4 bits
//start ADC conversion
ADCSRA |= (1 << ADSC);
//wait for conversion to finish
while(!(ADCSRA & (1 << ADIF)));
ADCSRA |= (1 << ADIF); //reset as required
return ADC;
}
int main(void)
{
uint32_t brightness_total;
uint16_t brightness = 0;
uint32_t i = 0;
init_adc();
sei();
while (1)
{
//reset LED pins
PORTB &= ~(1 << PINB0);
PORTD &= ~(1 << PIND7);
PORTD &= ~(1 << PIND6);
PORTD &= ~(1 << PIND5);
PORTB |= (1 << PINB1); //just blink
read_adc(BRIGHTNESS_PIN); //first throw-away read
//read n sample values from the ADC and average them out
brightness_total = 0;
for(i = 0; i < ADC_SAMPLES; ++i)
{
brightness_total += read_adc(BRIGHTNESS_PIN);
}
brightness = brightness_total / ADC_SAMPLES;
//set pins for LEDs depending on read value.
if(brightness > 768)
{
PORTB |= (1 << PINB0);
PORTD |= (1 << PIND7);
PORTD |= (1 << PIND6);
PORTD |= (1 << PIND5);
}
else if (brightness <= 768 && brightness > 512)
{
PORTB |= (1 << PINB0);
PORTD |= (1 << PIND7);
PORTD |= (1 << PIND6);
}
else if (brightness <= 512 && brightness > 256)
{
PORTB |= (1 << PINB0);
PORTD |= (1 << PIND7);
}
else if (brightness <= 256 && brightness >=64)
{
PORTB |= (1 << PINB0);
}
_delay_ms(500);
PORTB &= ~(1 << PINB1); //just blink
_delay_ms(500);
}
}
This works kind of fine, except the channel selection. When I select a channel it works fine, but independently from the selected channel, channel 0 also always reads and converts. What I mean with that is that if I plug the cable into the selected channel pin, it reads the values correctly. When I plug it into any other channel pin it obviously doesn't, except for ADC0. No matter what channel I set, not only does that one read but also ADC0.
Why is that and how do I fix that?
I already checked my PCB for solder bridges, but there are none and I would also expect some slightly different behaviour with that.
Also ADC4 and ADC5 don't seem to properly convert either. Any idea why that is? The only clue I found in the datasheet is, that those two use digital power, while all the other ADCs use analogue. What's the difference, why does it matter and why does it not correctly convert my anlogue signal?
Both ARef and AVCC are connected according to the datasheet, with the exception that the inductor for ARef is missing.
I think what is happening is that
ADMUX &= (0xF0);
is setting the channel to 0, and
ADMUX |= (channel & 0x0F);
is then setting the channel to the one you want. You're then taking a reading and throwing the result away, which should mean that the initial channel being set to 0 doesn't matter.
Howevever, when you then try to take an actual reading, you are setting the channel again, by using read_adc to take the reading. So, you don't ever throw a reading away.
What I would do is replace your ADMUX setting commands with:
ADMUX = (0xF0) | (channel & 0x0F)
Then move this into a separate function called something like set_adc_channel(int channel). Include a throw away read in that function, then remove the ADMUX setting from your read_adc function. Just start a conversion and get the result.
Also note that since you only ever use one channel, you could move the channel setting part to init_adc(). I assume it's in a separate function so you could later read more than one channel.
I hope that's clear. Let me know if not.
EDIT: So as you stated, ADIF is really reset by writing logic 1.
I've just tested your adc_read function and it is working for me (if you don't mind Arduino mixture)
uint16_t read_adc(unsigned char channel)
{
//clear lower 4 bits of ADMUX and select ADC channel according to argument
ADMUX &= (0xF0);
ADMUX |= (channel & 0x0F); //set channel, limit channel selection to lower 4 bits
//start ADC conversion
ADCSRA |= (1 << ADSC);
//wait for conversion to finish
while(!(ADCSRA & (1 << ADIF)));
ADCSRA |= (1 << ADIF); //reset as required
return ADC;
}
void setup() {
Serial.begin(57600);
//set ADC VRef to AVCC
ADMUX |= (1 << REFS0);
//enable ADC and set pre-scaler to 128
ADCSRA = (1 << ADPS0) | (1 << ADPS1) | (1 << ADPS2) | (1 << ADEN);
pinMode(A0, INPUT_PULLUP);
pinMode(A1, INPUT_PULLUP);
pinMode(A2, INPUT_PULLUP);
pinMode(A3, INPUT_PULLUP);
}
void loop() {
Serial.println(read_adc(0));
Serial.println(read_adc(1));
Serial.println(read_adc(2));
Serial.println(read_adc(3));
delay(1000);
}
I just connect one of these channels to 3.3V pin and it'll read 713 on it. Other channels are pulled up to levels about 1017.