Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "config.h"
- #include <avr/io.h>
- #include <util/delay.h>
- #include <avr/interrupt.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- #include "OneWire.h"
- #include "LCD.h"
- //#define F_CPU 8000000UL
- #define MOSI 3
- #define MISO 4
- #define SCK 5
- #define SS 2
- #define SPE 6
- #define DORD 5
- #define MSTR 4
- #define CPOL 3
- #define CPHA 2
- #define SPR1 1
- #define SPR0 0
- #define SPIF 7
- inline void explodeDoubleNumber(int* numbers, double flt) {
- numbers[0] = abs((int) flt);
- numbers[1] = abs((int) ((flt - ((int) flt)) * 10));
- }
- inline void printTemp(double d, uint8_t i) {
- char text[17] = "T[";
- int fs[2];
- char num[5];
- itoa(i, num, 10);
- strcat(text, num);
- strcat(text, "]=");
- explodeDoubleNumber(fs, d);
- if (d < 0) {
- strcat(text, "-");
- }
- itoa(fs[0], num, 10);
- strcat(text, num);
- strcat(text, ".");
- itoa(fs[1], num, 10);
- strcat(text, num);
- strcat(text, "'C");
- lcdClear();
- lcdGotoXY(0, 0);
- lcdPuts(text);
- }
- double getTemp(uint64_t ds18b20s) {
- uint8_t temperatureL;
- uint8_t temperatureH;
- double retd = 0;
- setDevice(ds18b20s);
- writeByte(CMD_CONVERTTEMP);
- _delay_ms(750);
- setDevice(ds18b20s);
- writeByte(CMD_RSCRATCHPAD);
- temperatureL = readByte();
- temperatureH = readByte();
- retd = ((temperatureH << 8) + temperatureL) * 0.0625;
- return retd;
- }
- uint8_t getTemp8(char LH) { //uint8_t getTemp8(uint64_t ds18b20s, char LH) {
- uint8_t temperatureL;
- uint8_t temperatureH;
- //setDevice(ds18b20s);
- skipRom();
- writeByte(CMD_CONVERTTEMP);
- _delay_ms(750);
- //setDevice(ds18b20s);
- skipRom();
- writeByte(CMD_RSCRATCHPAD);
- temperatureL = readByte();
- temperatureH = readByte();
- if(LH == 'l'){
- return temperatureL;
- } else if (LH == 'h'){
- return temperatureH;
- }
- return 0;
- }
- void SPI_SLave_init(void)
- {
- DDRB &= ~(1<<SCK)|~(1<<MOSI)|~(1<<SS);//MISO как вход
- DDRB |=(1<<MISO);
- SPCR=0;//Обнулить регистр SPCR
- SPSR=0;//Обнулить регистр SPSR
- SPCR &= ~(1<<MSTR); //режим слейв?
- SPCR |= (1<<SPIE);
- //SPCR |= (1<<SPI2X);
- SPCR |= (1<<CPOL);
- SPCR |= (1<<CPHA);
- SPCR |= (1<<SPR0);//не надо
- SPCR |= (1<<SPR1);//не надо
- //SPCR |= (1<<DORD);
- SPCR &=~ (1<<DORD);
- SPCR |=(1<<SPE);
- }
- uint8_t SPI_Slave_Receive(void)
- {
- while(!(SPSR&(1<<SPIF)));/* Ожидание завершения приема: */
- return SPDR;/* Чтение принятых данных и возврат: */
- }
- uint8_t SPI_Slave_Transmit(uint8_t cData)
- {
- uint8_t report;
- PORTB &=~(1<<SS); //Установить "0" на линии SS
- SPDR = cData; /* Запуск передачи данных: */
- while(!(SPSR & (1<<SPIF))); /* Ожидание завершения передачи: */
- report = SPDR;
- PORTB |=(1<<SS); //Установить "1" на линии SS
- return report;
- }
- uint8_t command;
- uint8_t temp_cache0;
- uint8_t temp_cache1;
- uint8_t numb;
- uint8_t temp_cache3;
- uint8_t temp_cache4;
- uint8_t temp_cache5;
- const int N = 1000;
- double retd = 0;
- ISR(SPI_STC_vect)//прерывание SPI прием байта
- {
- command=SPDR;
- // uint8_t n = 8;
- // uint64_t roms[n];
- // searchRom(roms, n);
- if (command == 0) {
- temp_cache0 = getTemp8('l');
- SPDR = temp_cache0;
- }
- else if (command == 1) {
- temp_cache1 = getTemp8('h');
- SPDR = temp_cache1;
- }
- else if (command == 2) {
- PORTC&=~(1<<PC0);
- } else if (command == 3){
- PORTC|=(1<<PC0);
- }
- }
- int main(void) {
- _delay_ms(100);
- SPI_SLave_init();
- sei();
- DDRC|=(1<<PC0);
- PORTC=0;
- oneWireInit(PIND7);
- while (1) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement