📄 test.c
字号:
/*********************************************
* Chip type : ATmega16
* Clock frequency : 12,000000 MHz
*********************************************/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/signal.h>
#include <inttypes.h>
#include <avr/iom16.h>
#include <stdio.h>
#include <stdlib.h>
#include <util/delay.h>
#define F_OSC 12000000l /* oscillator-frequency in Hz */
#define UART_BAUD_RATE 9600
#define UART_BAUD_CALC(UART_BAUD_RATE,F_OSC) ((F_OSC)/((UART_BAUD_RATE)*16l)-1)
#define prevod 0x44
#define rdscr 0xbe
#define wrscr 0x4e
#define coscr 0x48
#define recall 0xb8
#define power 0xb4
#define hledat 0xf0
#define cistrom 0x33
#define pripoj 0x55
#define bezrom 0xcc
#define alarm 0xec
/* constants */
#define rozliseni 625 //.0625
#define cidlo1 0x289E4EB4010000C3
#define cidlo2 0x28F94DB4010000CB
#define cidlo3 0x28DE6CB4010000CD
char i;
char a,b;
void usart_putc(unsigned char c) {
// wait until UDR ready
while(!(UCSRA & (1 << UDRE)));
UDR = c; // send character
PORTA ^= 0xAA;
_delay_ms(10);
PORTA ^= 0xAA;
}
void uart_puts (const char *s) {
// loop until *s != NULL
while (*s) {
usart_putc(*s);
s++;
}
}
void uart_puti( const int val )
{
char buffer[sizeof(int)*8+1];
uart_puts( itoa(val, buffer, 10) );
}
void uart_puthex_nibble(const unsigned char b)
{
unsigned char c = b & 0x0f;
if (c>9) c += 'A'-10;
else c += '0';
usart_putc(c);
}
void uart_puthex_byte(const unsigned char b)
{
uart_puthex_nibble(b>>4);
uart_puthex_nibble(b);
}
void init(void) {
// set baud rate
UBRRH = (uint8_t)(UART_BAUD_CALC(UART_BAUD_RATE,F_OSC)>>8);
UBRRL = (uint8_t)UART_BAUD_CALC(UART_BAUD_RATE,F_OSC);
//UBRRH = (uint8_t)(a>>8);
//UBRRL = (uint8_t)(a);
// Enable receiver and transmitter; enable RX interrupt
UCSRB = (1 << RXEN) | (1 << TXEN) | (1 << RXCIE);
//asynchronous 8N1
UCSRC = (1 << URSEL) | (3 << UCSZ0);
}
// INTERRUPT can be interrupted
// SIGNAL can't be interrupted
SIGNAL (SIG_UART_RECV) { // USART RX interrupt
unsigned char c;
c = UDR;
usart_putc(c);
}
void vstup(void) {
DDRC&=~(1<<PC7);
}
void odpoj(void) {
DDRC|=(1<<PC7);
}
void dolu(void) {
PORTC&=~(1<<PC7);
}
void nahoru(void) {
PORTC|=(1<<PC7);
}
uint8_t in18b20(void)
{
uint8_t i;
dolu();
odpoj();
_delay_us(480);
vstup();
_delay_us(60);
i=(PINC & (1<<PC7));
_delay_us(420);
return i;
}
void wr18b20(uint8_t bit)
{
dolu();
odpoj();
_delay_us(1);
if(bit) vstup();
_delay_us(60);
vstup();
}
uint8_t rd18b20(void)
{
uint8_t bit=0;
dolu();
odpoj();
_delay_us(1);
vstup();
_delay_us(14);
if(PINC&(1<<PC7)) bit=1;
_delay_us(45);
return bit;
}
uint8_t rdbyte(void)
{
uint8_t i=8, n=0;
while(i--)
{
n>>=1;
n|=(rd18b20()<<7);
}
return n;
}
void wrbyte(uint8_t byte)
{
uint8_t i=8;
while(i--)
{
wr18b20(byte&1);
byte>>=1;
}
}
void call18b20 (uint64_t bytes) {
uint8_t i=8;
unsigned char byt;
wrbyte(pripoj);
while(i--)
{byt = (bytes >> 56);
bytes <<= 8;
wrbyte(byt);
//uart_puthex_byte(byt);
}
}
char teplota(char *buffer, uint64_t bytes)
{
uint8_t temp[2];
int8_t digit;
uint16_t decimal;
in18b20();
//wrbyte(bezrom);
call18b20(bytes);
wrbyte(prevod);
while(!rd18b20());
in18b20();
//wrbyte(bezrom);
call18b20(bytes);
wrbyte(rdscr);
temp[0]=rdbyte();
temp[1]=rdbyte();
in18b20();
digit=temp[0]>>4;
digit|=(temp[1]&0x7)<<4;
decimal=temp[0]&0xf;
decimal*=rozliseni;
if ((temp[0]==0xFF) && (temp[1]==0xFF)) return 0; else {sprintf(buffer, "%+d.%04u C", digit, decimal); return 1;}
}
void najdi(void)
{
//char adr[4];
char adres[20]="";
in18b20();
//wrbyte(0xcc);
wrbyte(cistrom);
for (i=0;i<8;i++){
adres[i*2]= rdbyte();
}
for (i=0;i<8;i++){
//sprintf(adres[i], "%x:", adres[i]);
uart_puthex_byte(adres[i*2]);
if (i<7) uart_puts(":");
}
in18b20();
}
int main(void) {
char x[10] = {0};
init(); // init USART
sei(); // enable interrupts
// send initial character
// while(!(UCSRA & (1 << UDRE)));
// UDR = 0x43; // "C"
//while(!(UCSRA & (1 << UDRE)));
//UDR = 0x0d;
// enable PA5 as output
DDRA = 0xff;
usart_putc(0x0c);
uart_puts("ATmega 16 ...");
usart_putc(0x0a);
usart_putc(0x0d);
uart_puts("----------------------");
usart_putc(0x0a);
usart_putc(0x0d);
uart_puts("Seriove spojeni --- baudrate 9600 kbps");
usart_putc(0x0a);
usart_putc(0x0d);
if (!in18b20()) uart_puts("Senzor inicializovan"); else uart_puts("Senzor nebyl inicializovan");
usart_putc(0x0a);
usart_putc(0x0d);
// uart_puts("Adresa senzoru je ");
// najdi();
// uart_puts(adr);
usart_putc(0x0a);
usart_putc(0x0d);
_delay_ms(1000);
while (1) {
// PIN5 PORTD clear -> LED off
//PORTA &= ~(1 << PA5);
// PIN5 PORTD set -> LED on
PORTA ^= 0x01;
_delay_ms(500);
uart_puts("Namerene teploty na cidlech jsou ");
if (teplota(x,cidlo1)) uart_puts(x); else uart_puts("Chyba");
PORTA ^= 0x01;
PORTA ^= 0x04;
_delay_ms(500);
uart_puts(" ");
if (teplota(x,cidlo2)) uart_puts(x); else uart_puts("Chyba");
PORTA ^= 0x04;
PORTA ^= 0x10;
_delay_ms(500);
uart_puts(" ");
if (teplota(x,cidlo3)) uart_puts(x); else uart_puts("Chyba");
PORTA ^= 0x10;
usart_putc(0x0a);
usart_putc(0x0d);
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -