Проектирование процедуры инициализации аппаратуры микроконтроллера
Поможем в ✍️ написании учебной работы
Поможем с курсовой, контрольной, дипломной, рефератом, отчетом по практике, научно-исследовательской и любой другой работой

 

Процедура инициализации производит настройку: портов ввода/вывода, периферийных аппаратных устройств, а так же внешних устройств которые требуют инициализации.

//== Port Initialisation ===============

void Init(void)

{

DDRD = 0xf0; //PD3-PD0 as input

PORTD = 0xff; //Turn ON PullUP for PortB pins

DDRB = 0xff; //Port B pins as output

PORTB = 0x00;

}

//== Virtual Timer Initialisation ==========

void InitTimers(void)

{

#asm("cli");

TCCR0B=PrescalerTmr0;

TIMSK |= (1 << TOIE0); //Enable Timer0 Interrupt

TCNT0=Tmr0_Reload;

TmrPreLoad[0]=250;

TmrCnt[0]=250;

TmrFlag[1]=0x81;

TmrPreLoad[1]=10;

#asm("sei");

}

//=================================

char TimeDelay_us(char x) //near 1us time delay

{

char i,j,k,n;

j=1;

for (i=0;i<x;i++)

{

k=j+1;

n=k-j;

}

return n;

}

//== Circular Buffer Write =====================

void CircBufPut (unsigned char data)

{

unsigned char tmphead;

tmphead = CircBufHead + 1;

if (tmphead>=CircBufLen)

{

tmphead=0;

}

CircBuf[tmphead] = data;

CircBufHead = tmphead;

}

//== Circular Buffer Read ==========

unsigned char CircBufGet(void)

{

unsigned char tmptail;

if (CircBufHead != CircBufTail)

{

tmptail=CircBufTail+1;

if (tmptail>=CircBufLen)

{

tmptail=0;

}

CircBufTail = tmptail;

return CircBuf[tmptail];

}

else

{

return 0;

}

}

 

Инициализация цифрового термометра DS1620

 

char DS1620Init(void)

{

char Presence;

DDRD |= 0x20;

PORTD &= ~0x20;

TimeDelay_us(200);

TimeDelay_us(200);

TimeDelay_us(200);

DDRB &= ~0x10;

PORTB |= 0x10;

TimeDelay_us(20);

Presence = PIND & 0x10;

TimeDelay_us(200);

DDRD |= 0x20;

PORTD |= 0x20;

TimeDelay_us(200);

return Presence;

}

void DS1620WriteBit(char Value)

{

#asm("cli");

DDRD |= 0x20; //output 5

PORTD &= ~0x20;

TimeDelay_us(5);

if(Value!=0) //if data bit = H => output 5

{

PORTD |= 0x20;

}

TimeDelay_us(70);

PORTD |= 0x20; //output 5

TimeDelay_us(5);

#asm("sei");

}

void DS1620WriteByte(char data)

{

char loop, CurrentBit;

for (loop = 0; loop < 8; loop++) // Loop to write each bit in the byte, LS-bit first

{

CurrentBit = data & 0x01;

DS1620WriteBit(CurrentBit);

data >>= 1; // shift the data byte for the next bit

}

}

char DS1620ReadBit(void)

{

char Value;

#asm("cli");

DDRD |= 0x20; //output 5

PORTD &= ~0x20;

TimeDelay_us(5);

DDRD &= ~0x20; //input

PORTD |= 0x20;

TimeDelay_us(10);

Value = PIND & 0x20; //read bit

TimeDelay_us(55);

DDRD |= 0x20; //output 5

PORTD |= 0x20;

TimeDelay_us(5);

#asm("sei");

return Value;

}

//==

char DS1620ReadByte(void)

{

char loop, result=0, CurrentBit;

for (loop = 0; loop < 8; loop++)

{

result >>= 1; // shift the result right to get it ready for the next bit

CurrentBit = DS1620ReadBit();

if (CurrentBit != 0) // if result is one, then set MS bit

{

result |= 0x80;

}

}

return result;

}

 

Инициализация и настройка ЖКИ

 

#include <tiny2313.h>

/*

#define LCD_E PORTC_Bit4

#define LCD_RW PORTC_Bit5

#define LCD_RS PORTC_Bit6

#define LCD_DATA PORTC

#define LCD_PIN PINC

#define LCD_DDR DDRC

*/

#define E PORTD.2

#define WR PORTD.1

#define RS PORTD.0

#define LCD_DATA PORTD

#define LCD_PIN PIND

#define LCD_DDR DDRD

#define CLRBIT(ADDR, BIT)   (ADDR |= (1<<BIT))

#define SETBIT(ADDR, BIT)  (ADDR &= ~(1<<BIT))

char ini_cmd[]={0x03,0x03,0x03,0x02,0x02,0x0d,0x00,0x0d,0x00,0x01,0x00,0x06};

//==================================

void Delay(int i) // программная задержка

{

while(--i>0x00);

}

//===============================

void SendDataToDisplay(unsigned char Data, unsigned char Mode)

{

//PORTB - 8bit Data

/*PORTD - PD0 - RS

PD1 - RW

PD2 - E */

CLRBIT(PORTD,E);

if (Mode)

SETBIT(PORTD,RS);

else

CLRBIT(PORTD,RS);

PORTB = Data;

CLRBIT(PORTD,WR);

SETBIT(PORTD,E);

Delay(4);

CLRBIT(PORTD,E);

}

//========================

void DisplayInit(void)

{

Delay(30);

SendDataToDisplay(0x30,1); //режим работы дисплея – ширина шины данных 8 бит

Delay(5);

SendDataToDisplay(0x30,1);

Delay(1);

SendDataToDisplay(0x30,1);

SendDataToDisplay(0x38,1); // шина данных 8 бит

//размер развертки 2 строки

//размер матр. Символов – 5х10

SendDataToDisplay(0x08,1); //выкл. Наличие изображения

SendDataToDisplay(1,1); //очистка экрана

SendDataToDisplay(0x6,1); //счетчик адреса настроить на увеличение

SendDataToDisplay(0xC,1); //вкл. изображение

}

unsigned char ReadDatafromDisplay(unsigned char Mode)

{

unsigned char a;

CLRBIT(PORTD, E);

if (Mode)

SETBIT(PORTD,RS);

else

CLRBIT(PORTD,RS);

DDRB &= 0x00; //установка порта на чтение

PORTB |= 0xFF;

Delay(4);

a = PORTB;

CLRBIT(PORTD,E);

return a;

}

 

Дата: 2019-07-30, просмотров: 147.