Skip to content

Commit

Permalink
fixed problems after moving libraries to separate module, fixed some …
Browse files Browse the repository at this point in the history
…strange warnings during compilation, disabled USART in dht11 module
  • Loading branch information
ppkt committed Jun 28, 2015
1 parent 6ec2829 commit c7a796b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 41 deletions.
36 changes: 18 additions & 18 deletions dht11.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ u32 interrupt_line;
dht11_state dht11_current_state = DHT11_NONE;
u16 dht11_pulse_lengths[6];

bool data[40];
bool dht11_data[40];
u8 data_pos = 0;

u8 last_temperature = 0;
Expand Down Expand Up @@ -136,27 +136,27 @@ bool dht11_check_tolerance(u32 timer) {
if ((timer >= min) && (timer <= max)) {
return true;
}
char tab[50];
sprintf(tab, "Error, expected: %d at position %d, received: %u %d\r\n", expected, data_pos, (unsigned int)timer, dht11_current_state);
printf(tab);
// char tab[50];
// sprintf(tab, "Error, expected: %d at position %d, received: %u %d\r\n", expected, data_pos, (unsigned int)timer, dht11_current_state);
// printf(tab);
return false;
}

void dht11_trigger_state_machine(u32 timer, u8 bit) {
u8 middle_value = (dht11_pulse_lengths[DHT11_RELEASE_0] + dht11_pulse_lengths[DHT11_RELEASE_1]) / 2;
dht11_check_tolerance(timer);

char tab[10];
sprintf(tab, "%u\r\n", (unsigned int)timer);
// char tab[10];
// sprintf(tab, "%u\r\n", (unsigned int)timer);

switch(dht11_current_state) {
case DHT11_NONE:
if (bit) {
break; // wrong state
}
GPIO_WriteBit(GPIOC, GPIO_Pin_13, Bit_SET);
// GPIO_WriteBit(GPIOC, GPIO_Pin_13, Bit_SET);
dht11_current_state = DHT11_INIT_PULL_DOWN;
GPIO_WriteBit(GPIOC, GPIO_Pin_13, Bit_RESET);
// GPIO_WriteBit(GPIOC, GPIO_Pin_13, Bit_RESET);
break;
case DHT11_INIT_PULL_DOWN:
dht11_current_state = DHT11_INIT_RELEASE;
Expand All @@ -172,9 +172,9 @@ void dht11_trigger_state_machine(u32 timer, u8 bit) {
case DHT11_RELEASE_0:
case DHT11_RELEASE_1:
if (timer < middle_value) {
data[data_pos] = 0;
dht11_data[data_pos] = 0;
} else {
data[data_pos] = 1;
dht11_data[data_pos] = 1;
}
++data_pos;

Expand All @@ -189,9 +189,9 @@ void dht11_trigger_state_machine(u32 timer, u8 bit) {
break;

case DHT11_EOT:
GPIO_WriteBit(GPIOC, GPIO_Pin_13, Bit_SET);
// GPIO_WriteBit(GPIOC, GPIO_Pin_13, Bit_SET);
dht11_current_state = DHT11_NONE;
GPIO_WriteBit(GPIOC, GPIO_Pin_13, Bit_RESET);
// GPIO_WriteBit(GPIOC, GPIO_Pin_13, Bit_RESET);
break;
default:
break;
Expand All @@ -211,18 +211,18 @@ void dht11_decode_data() {

u8 temp_integral = 0, temp_decimal = 0, rh_integral = 0, rh_decimal = 0, checksum = 0;
for (i = 0; i < 8; ++i) {
rh_integral |= data[i] << (7 - i);
rh_decimal |= data[8 + i] << (7 - i);
temp_integral |= data[16 + i] << (7 - i);
temp_decimal |= data[24 + i] << (7 - i);
checksum |= data[32 + i] << (7 - i);
rh_integral |= dht11_data[i] << (7 - i);
rh_decimal |= dht11_data[8 + i] << (7 - i);
temp_integral |= dht11_data[16 + i] << (7 - i);
temp_decimal |= dht11_data[24 + i] << (7 - i);
checksum |= dht11_data[32 + i] << (7 - i);
}
// sprintf(tab, "%d.%d %d.%d %d\r\n", temp_integral, temp_decimal, rh_integral, rh_decimal, checksum);
if (temp_integral + temp_decimal + rh_integral + rh_decimal == checksum) {
last_temperature = temp_integral;
last_relative_humidity = rh_integral;
} else {
printf("Checksum incorrect\r\n");
// printf("Checksum incorrect\r\n");
last_temperature = 0;
last_relative_humidity = 0;
}
Expand Down
2 changes: 1 addition & 1 deletion dht11.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef __DHT11_H__
#define __DHT11_H__
#include "stdbool.h"
#include "stdio.h"
//#include "stdio.h"

#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
Expand Down
46 changes: 26 additions & 20 deletions hd44780-i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ u8 en = 1 << 2;
u8 rw = 1 << 1;
u8 rs = 1 << 0;
TIM_TypeDef *timer;
uint8_t data[4] = {0x00, 0x00, 0x00};
uint8_t hd44780_data[4] = {0x00, 0x00, 0x00, 0x00};

void hd44780_send(u8 cmd, bool set_rs) {
u8 rs_ = 0;
if (set_rs)
rs_ = rs;

data[0] = (cmd & 0xF0) | backlight | en | rs_;
data[1] = (cmd & 0xF0) | backlight;
data[2] = (cmd & 0x0F) << 4 | backlight | en | rs_;
data[3] = (cmd & 0x0F) << 4 | backlight;
I2C_Master_BufferWrite(I2C1, data, 4, Polling, hd44780_address << 1);
hd44780_data[0] = (cmd & 0xF0) | backlight | en | rs_;
hd44780_data[1] = (cmd & 0xF0) | backlight;
hd44780_data[2] = (cmd & 0x0F) << 4 | backlight | en | rs_;
hd44780_data[3] = (cmd & 0x0F) << 4 | backlight;
I2C_Master_BufferWrite(I2C1, hd44780_data, 4, Polling, hd44780_address << 1);
}

void hd44780_char(u8 cmd) {
Expand All @@ -54,9 +54,9 @@ void hd44780_print(char *string) {

void hd44780_backlight(bool new_value) {
backlight = new_value << 3;
I2C_Master_BufferRead(I2C1, data, 1, Polling, hd44780_address << 1);
data[0] |= backlight;
I2C_Master_BufferWrite(I2C1, data, 1, Polling, hd44780_address << 1);
I2C_Master_BufferRead(I2C1, hd44780_data, 1, Polling, hd44780_address << 1);
hd44780_data[0] |= backlight;
I2C_Master_BufferWrite(I2C1, hd44780_data, 1, Polling, hd44780_address << 1);
}

void hd44780_go_to_line(u8 line) {
Expand Down Expand Up @@ -85,46 +85,49 @@ void hd44780_go_to(u8 row, u8 col) {
hd44780_cmd(0x14);
}

void hd44780_cgram_write(u8 pos, u8 data[8]) {
void hd44780_cgram_write(u8 pos, u8 data_[8]) {
if (pos > 7) {
return;
}
pos = 64 + pos * 8;
hd44780_cmd(pos);
u8 i;
for (i = 0; i < 8; ++i) {
hd44780_send(data[i], true);
hd44780_send(data_[i], true);
}
}

void hd44780_init(TIM_TypeDef *t) {
timer = t;
// Setup clock
if (timer == TIM2)
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
else if (timer == TIM3)
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
else
while(1){} // not implemented
while(1){} // not implemented
TIM_TimeBaseInitTypeDef TIM_InitStructure;
TIM_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_InitStructure.TIM_Prescaler = 72 - 1;
TIM_InitStructure.TIM_Prescaler = (SystemCoreClock) / 1000000 - 1;
TIM_InitStructure.TIM_Period = 10000 - 1; // Update event every 10000 us / 10 ms
TIM_InitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_InitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(timer, &TIM_InitStructure);
TIM_Cmd(timer, ENABLE);


data[0] = 0x00 | backlight;
data[1] = 0x00 | backlight;
hd44780_data[0] = 0x00 | backlight;
hd44780_data[1] = 0x00 | backlight;

// GPIO_SetBits(GPIOC, GPIO_Pin_8);
// delay_us(timer, 250);
// GPIO_ResetBits(GPIOC, GPIO_Pin_8);

// Init I2C
I2C_LowLevel_Init(I2C1);

// Reset all
I2C_Master_BufferWrite(I2C1, data, 1, Polling, hd44780_address << 1);
I2C_Master_BufferWrite(I2C1, hd44780_data, 1, Polling, hd44780_address << 1);

hd44780_cmd(0x03);
delay_us(timer, 5000);
Expand All @@ -139,12 +142,15 @@ void hd44780_init(TIM_TypeDef *t) {
hd44780_cmd(0x0E); // turn on display, set solid cursor
hd44780_cmd(0x0C); // turn on display, set invisiblecursor

hd44780_cgram_write(0, (u8[]){0,10,31,31,14,4,0,0});
hd44780_cgram_write(1, (u8[]){0,10,21,17,10,4,0,0});
u8 arr[] = {0,10,31,31,14,4,0,0};
u8 arr2[]= {0,10,21,17,10,4,0,0};

hd44780_cgram_write(0, arr);
hd44780_cgram_write(1, arr2);

hd44780_cmd(0x01); // clear display, go to 0x0

// hd44780_backlight(true);
hd44780_backlight(true);
// hd44780_print("Linia 0");
// hd44780_go_to_line(1);
// hd44780_print("Linia 1");
Expand Down
7 changes: 5 additions & 2 deletions hd44780-i2c.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
#ifndef __HD44780_I2C_H__
#define __HD44780_I2C_H__
#include "utils.h"
#include "i2c_dma.h"
#include "stdbool.h"

#include <stm32f10x.h>
#include <stm32f10x_tim.h>

#include "common_lib/utils.h"
#include "common_lib/i2c_dma.h"

#define hd44780_address 0x27
void hd44780_init(TIM_TypeDef *timer);
void hd44780_print(char *string);
void hd44780_go_to_line(u8 line);
void hd44780_go_to(u8 row, u8 col);
void hd44780_cmd(u8 cmd);
void hd44780_char(u8 c);


#endif // __HD44780_I2C_H__

0 comments on commit c7a796b

Please sign in to comment.