|
|
Elektronika.lt portalo forumas
Jūs esate neprisijungęs lankytojas. Norint dalyvauti diskusijose, būtina užsiregistruoti ir prisijungti prie forumo.
Prisijungę galėsite kurti naujas temas, atsakyti į kitų užduotus klausimus, balsuoti forumo apklausose.
Administracija pasilieka teisę pašalinti pasisakymus bei dalyvius,
kurie nesilaiko forumo taisyklių.
Pastebėjus nusižengimus, prašome pranešti.
Dabar yra 2024 11 26, 02:26. Visos datos yra GMT + 2 valandos.
|
|
|
|
Forumas » Mikrovaldikliai » Prasymas Pas tuos kad programuoja su C
|
Jūs negalite rašyti naujų pranešimų į šį forumą Jūs negalite atsakinėti į pranešimus šiame forume Jūs negalite redaguoti savo pranešimų šiame forume Jūs negalite ištrinti savo pranešimų šiame forume Jūs negalite dalyvauti apklausose šiame forume
|
|
|
|
Prasymas Pas tuos kad programuoja su C |
Parašytas: 2005 05 30, 20:26 |
|
|
|
reikia pagalbos, man reikia uzprogramuot atme16 arba at89c51 (jokio skirtumo, gali but panasus i sius), tas mikrokontroleris jungiasi su mobiliaku.
Gal kas turit pavyzdziu skirtu tokiems kontroleriams(panasiems).
butu dar geriau jei pvz butu mikrovaldiklis kazka darytu su telefono (mobiliu)
jei tokio pvz neturit, tai siuskit bet kokius programu pavyzdzius skirtus atme16 arba at89c51 (ir panasiems) kontroleriams.
Labai jums aciu
zasinas@xaker.ru |
|
|
|
|
|
Prasymas Pas tuos kad programuoja su C |
Parašytas: 2005 05 30, 20:29 |
|
|
|
Na bet matosi kad žmogus neturi net žalio supratimo ko prašo. O tuo labiau aš neįsivaizduoju ko jis nori...
O kas per valdiklis atme16 Naują rūšį išgimdei? |
|
|
|
|
|
Prasymas Pas tuos kad programuoja su C |
Parašytas: 2005 05 30, 22:43 |
|
|
|
na skubujau rasyt tai ATSIPRASAU, taip valdiklis ATMEGA16
Man reikia pavysdziu programu.
programos parasytos su C kalba.
mikrokontroleriams ATMEGA16 ir panasiems.
pavyzdziu tema: kai mikrokontroleris sujunktas su mobiliaku ir kanors daro,
nusiunti SMS i mobilika o kontroleris kanors ijungia.
TOkio aisku niekas neturesit.
arba kai prijuktas DS18s20 arba DS1821 (termometrai)
arba LM555 (itampos keitiklis i dazni)
geriau kad PVZ butu paprasti, tam kad galeciau paziuret kokios komandos naudojamas ir aplamai kaip vyksta programavimas kontroleriu su C kalba.
stai tokiam dalykui reikia.
ACIU |
|
|
|
|
|
Prasymas Pas tuos kad programuoja su C |
Parašytas: 2005 05 30, 23:14 |
|
|
|
Gal tu eik geriau tutorialą skaityt ir nesiteisink, kad skubėjai, bo dviejose vietose identiškos klaidos. Čia jau ne skubėjimas... |
|
|
|
|
|
|
Prasymas Pas tuos kad programuoja su C |
Parašytas: 2005 05 31, 00:12 |
|
|
|
Norėjai bet ko ? Turėk:
Kodas: |
//*************************************************************//
// TEMPERATURE REGULATOR FOR SOLDERING IRON V1.0 beta //
// //
// //
// Copyright (c) 2005 Gediminas Labutis //
// //
// Permission is hereby granted, free of charge, to any //
// person obtaining a copy of this software and associated //
// documentation files (the "Software"), to deal in the //
// Software without restriction, including without //
// limitation the rights to use, copy, modify, merge, publish, //
// distribute, sublicense, and/or sell copies of the Software, //
// and to permit persons to whom the Software is furnished to //
// do so, subject to the following conditions: //
// //
// The above copyright notice and this permission notice //
// shall be included in all copies or substantial portions of //
// the Software. //
// //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF //
// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED //
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A //
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL //
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, //
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF //
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, //
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR //
// OTHER DEALINGS IN THE SOFTWARE. //
//*************************************************************//
// //
// PB7 PB6 PB5 PB4 PB3 PB2 PB1 PB0 pins //
// DP G F E D C B A segments //
// 1 1 0 0 0 0 0 0 0xC0 0 //
// 1 1 1 1 1 0 0 1 0xF9 1 //
// 1 0 1 0 0 1 0 0 0xA4 2 //
// 1 0 1 1 0 0 0 0 0xB0 3 //
// 1 0 0 1 1 0 0 1 0x99 4 //
// 1 0 0 1 0 0 1 0 0x92 5 //
// 1 0 0 0 0 0 1 0 0x82 6 //
// 1 1 1 1 1 0 0 0 0xF8 7 //
// 1 0 0 0 0 0 0 0 0x80 8 //
// 1 0 0 1 0 0 0 0 0x90 9 //
// //
//*************************************************************//
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/signal.h>
#include <avr/interrupt.h>
#include <inttypes.h>
#include <avr/eeprom.h>
#define bit_get(p,m) ((p) & (m))
#define bit_flip(p,m) ((p) ^= (m))
#define bit_set(p,m) ((p) |= (m))
#define bit_clear(p,m) ((p) &= ~(m))
#define BIT(x) (0x01 << (x))
#define TRIAC 4 // PC4
#define Q1 5 // PD5
#define Q2 4 // PD4
#define Q3 3 // PD3
#define BUTTON_PLUS 2 // PD2
#define BUTTON_MINUS 1 // PD1
#define BUTTON_ON_OFF 0 // PD0
#define HOLDER 6 // PD6
// Button status
#define NOPUSH 0 // Button not pushed
#define PUSHED 1 // Button pushed
#define SHORT 2 // Short push
#define LONG 3 // Long push
#define HANDLED 4 // Button status handled
// Buttons port status
#define NOT_READED 0 // Buttons port readed
#define READED 1 // Buttons port not readed
#define SHORT_PUSH_COUNTER 30 //
#define LONG_PUSH_COUNTER 300 //
#define AUTO_COUNTER 50 // auto increment/decrement counter
#define NEW_VALUE 1
#define OLD_VALUE 0
#define COMPARATOR_COUNTER 3
#define OFF 0
#define ON 1
#define STANDBY 2
#define POWER_OFF 3
#define STANDBY_PERCENT 30
#define OFF_TIME 30 // After OFF_TIME minutes of inactivity regulator switches to OFF mode.
#define OFF_TIME_COUNTER (5*60*OFF_TIME)
#define UP 1
#define DOWN 0
uint8_t volatile buttons_port ;
uint8_t volatile buttons_port_status ;
uint8_t volatile percent_value;
uint8_t numbers[13] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xFF,0x8E,0x83} ;
// 0 1 2 3 4 5 6 7 8 9 OFF F b
uint8_t button_plus_status ;
uint16_t button_plus_counter ;
uint8_t button_minus_status ;
uint16_t button_minus_counter ;
uint8_t button_on_off_status ;
uint16_t button_on_off_counter ;
uint8_t percent ;
uint8_t temp,temp1;
uint8_t mode ; // ON, OFF, STANDBY
uint16_t off_counter ; // auto OFF counter
uint8_t holder_status ; // UP , DOWN
//*****************************************************************************************
void port_init(void)
{
PORTB = 0xFF;
DDRB = 0xFF;
PORTC = 0x00;
DDRC = 0x30;
PORTD = 0x07;
DDRD = 0x38;
}
//Watchdog initialize
// prescale: 16K
void watchdog_init(void)
{
wdt_reset() ;//this prevents a timeout on enabling
WDTCR = 0x08; //WATCHDOG ENABLED - dont forget to issue WDRs
}
//TIMER0 initialize - prescale:256
// desired value: 150Hz
// actual value: 150,240Hz (0,2%)
void timer0_init(void)
{
TCCR0 = 0x00; //stop
TCNT0 = 0xE6; //set count
TCCR0 = 0x04; //start timer
}
SIGNAL(SIG_OVERFLOW0) // TIMER0 interrupt
{
TCNT0 = 0xE6; //reload counter value
uint8_t static first_digit ;
uint8_t static second_digit ;
uint8_t static third_digit ;
uint8_t static digit_position ; //
buttons_port = PIND ;
buttons_port_status = READED ;
if(digit_position>=3) // if all digits handled
{ // calculate digits again
third_digit= percent % 10;
temp= percent / 10;
second_digit= temp % 10;
first_digit= temp / 10;
if (percent<100)
first_digit = 10 ; // all segments off
if (percent<10)
second_digit = 10 ;// all segments off
digit_position=0 ;
}
if ( mode == OFF)
{ // Indicate OFF
first_digit = 0 ; // O
second_digit = 11 ; // F
third_digit = 11 ; // F
}
else if ( mode == STANDBY)
{ // Indicate Sb
first_digit = 10 ; // OFF
second_digit = 5 ; // S on 7 sgment LED the same as 5
third_digit = 12 ; // b
}
switch(digit_position)
{
case 0 :
PORTB = numbers[first_digit] ;
bit_set(PORTD,BIT(Q1)); // Q1 ON
bit_clear(PORTD, BIT(Q2) | BIT(Q3)); // Q2,Q3 OFF
digit_position++;
break;
case 1 :
PORTB = numbers[second_digit] ;
bit_set(PORTD,BIT(Q2)); // Q2 ON
bit_clear(PORTD, BIT(Q1) | BIT(Q3)); // Q1,Q3 OFF
digit_position++;
break;
case 2 :
PORTB = numbers[third_digit] ;
bit_set(PORTD,BIT(Q3)); // Q3 ON
bit_clear(PORTD, BIT(Q1) | BIT(Q2)); // Q1,Q2 OFF
digit_position++;
break;
default:
digit_position=0;
PORTB = numbers[10] ; // all segments OFF
bit_clear(PORTD, BIT(Q1) | BIT(Q2) | BIT(Q3)); // Q1,Q2,Q3 OFF
}
}
//*******************************************
//TIMER1 initialize - prescale:256
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 5Hz
// actual value: 5,002Hz (0,0%)
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0xFC; //setup
TCNT1L = 0xF3;
OCR1AH = 0x03;
OCR1AL = 0x0D;
OCR1BH = 0x03;
OCR1BL = 0x0D;
ICR1H = 0x03;
ICR1L = 0x0D;
TCCR1A = 0x00;
TCCR1B = 0x04; //start Timer
}
SIGNAL(SIG_OVERFLOW1) // TIMER1 interrupt
{
int16_t static i,e ;
uint8_t percent_temp ;
if ( mode == STANDBY)
percent_temp = STANDBY_PERCENT;
else
percent_temp = percent ;
// TIMER1 has overflowed
TCNT1H = 0xFC; //reload counter high value
TCNT1L = 0xF3; //reload counter low value
// Triac control
if ((i>99) | (percent_value == NEW_VALUE)) //
{
i=0;
e=2*percent_temp-100;
percent_value = OLD_VALUE;
}
i++;
if (e>=0)
{
e=e+2*(percent_temp-100);
if ( (mode == ON) | (mode == STANDBY))
bit_set(PORTC, BIT(TRIAC));
else
bit_clear(PORTC, BIT(TRIAC));
}
else
{
e=e+2*percent_temp ;
bit_clear(PORTC, BIT(TRIAC));
}
}
//*******************************************
//Comparator initialize
// trigger on: Output toggle
void comparator_init(void)
{
ACSR = ACSR & 0xF7; //ensure interrupt is off before changing
ACSR = 0x40;
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
cli(); //disable all interrupts
port_init();
// watchdog_init();
timer0_init();
timer1_init();
// comparator_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x05; //timer interrupt sources
sei(); //re-enable interrupts
//all peripherals are now initialized
}
//
//*****************************************************************************************
void check_buttons ( uint8_t buttons_port)
{
if (!(bit_get(buttons_port,BIT(HOLDER)))) // Soldering iron on holder
holder_status = DOWN ;
else
holder_status = UP ;
// button +
if (!(bit_get(buttons_port,BIT(BUTTON_PLUS)))) // button is down ....
{
if (button_plus_status == NOPUSH) // is this a new push ?
{
cli();
button_plus_counter = 0; // yes - start the debounce timer
sei();
button_plus_status = PUSHED; // note that it's down
}
if (button_plus_status == PUSHED) // button is down - have we already noted that ?
{
if (button_plus_counter< 1000) // overflow protection
{
cli();
button_plus_counter++; // yes - inc the debounce timer
sei();
}
if (button_plus_counter >= LONG_PUSH_COUNTER)
{ // long enough for a LONG push ?
button_plus_status = LONG; // yes - note a LONG push
}
}
}
else
{
if (button_plus_status == PUSHED)
{ // button is up, was it pushed before ?
if (button_plus_counter > SHORT_PUSH_COUNTER)
{ // yes, was it long enough for a short push ?
button_plus_status = SHORT; // past bounce timeout, was a short push
}
else button_plus_status = NOPUSH; // no, too short - was a bounce
}
if ((button_plus_status == HANDLED) |(button_plus_status == LONG) )
{ // button is up, did we handle it ?
button_plus_status = NOPUSH; // yes - reset back to waiting for push
}
}
// button -
if (!(bit_get(buttons_port,BIT(BUTTON_MINUS)))) // button is down ....
{
if (button_minus_status == NOPUSH) // is this a new push ?
{
cli();
button_minus_counter = 0; // yes - start the debounce timer
sei();
button_minus_status = PUSHED; // note that it's down
}
if (button_minus_status == PUSHED) // button is down - have we already noted that ?
{
if (button_minus_counter< 1000) // overflow protection
{
cli();
button_minus_counter++; // yes - inc the debounce timer
sei();
}
if (button_minus_counter >= LONG_PUSH_COUNTER)
{ // long enough for a LONG push ?
button_minus_status = LONG; // yes - note a LONG push
}
}
}
else
{
if (button_minus_status == PUSHED)
{ // button is up, was it pushed before ?
if (button_minus_counter > SHORT_PUSH_COUNTER)
{ // yes, was it long enough for a short push ?
button_minus_status = SHORT; // past bounce timeout, was a short push
}
else button_minus_status = NOPUSH; // no, too short - was a bounce
}
if ((button_minus_status == HANDLED) |(button_minus_status == LONG) )
{ // button is up, did we handle it ?
button_minus_status = NOPUSH; // yes - reset back to waiting for push
}
}
// button ON/OFF
if (!(bit_get(buttons_port,BIT(BUTTON_ON_OFF)))) // button is down ....
{
if (button_on_off_status == NOPUSH) // is this a new push ?
{
cli();
button_on_off_counter = 0; // yes - start the debounce timer
sei();
button_on_off_status = PUSHED; // note that it's down
}
if (button_on_off_status == PUSHED) // button is down - have we already noted that ?
{
if (button_on_off_counter< 1000) // overflow protection
{
cli();
button_on_off_counter++; // yes - inc the debounce timer
sei();
}
}
}
else
{
if (button_on_off_status == PUSHED)
{ // button is up, was it pushed before ?
if (button_on_off_counter > SHORT_PUSH_COUNTER)
{ // yes, was it long enough for a short push ?
button_on_off_status = SHORT; // past bounce timeout, was a short push
}
else button_on_off_status = NOPUSH; // no, too short - was a bounce
}
if (button_on_off_status == HANDLED)
{ // button is up, did we handle it ?
button_on_off_status = NOPUSH; // yes - reset back to waiting for push
}
}
}
//*****************************************************************************************
//*****************************************************************************************
void status_handling(void)
{
uint16_t static auto_inc_dec_ct ;
// increment percent value if:
// button PLUs short push AND button MINUS not pushed AND regulator mode ON
if ((button_plus_status == SHORT) & (button_minus_status == NOPUSH) & ( mode == ON))
{
if (percent < 100 )
{
cli();
percent++;
percent_value = NEW_VALUE;
sei();
}
button_plus_status = HANDLED ;
}
// decrement percent value if:
// button MINUS short push AND button PLUS not pushed AND regulator mode ON
if ((button_minus_status == SHORT) & (button_plus_status == NOPUSH) & ( mode == ON))
{
if (percent > 0 )
{
cli();
percent--;
percent_value = NEW_VALUE;
sei();
}
button_minus_status = HANDLED ;
}
// auto incremet percent value if:
// button PLUS long push AND button MINUS not pushed AND regulator mode ON
if ((button_plus_status == LONG) & (button_minus_status == NOPUSH) & ( mode == ON))
{
if (auto_inc_dec_ct < 1000) // overflow protection
{
cli();
auto_inc_dec_ct ++;
sei();
}
if (auto_inc_dec_ct > AUTO_COUNTER )
{
cli();
auto_inc_dec_ct = 0;
sei();
if (percent < 100 )
{
cli();
percent++;
percent_value = NEW_VALUE;
sei();
}
}
}
// auto decremet percent value if:
// button MINUS long push AND button PLUS not pushed AND regulator mode ON
if ((button_minus_status == LONG) & (button_plus_status == NOPUSH) & ( mode == ON) )
{
if (auto_inc_dec_ct < 1000) // overflow protection
{
cli();
auto_inc_dec_ct ++;
sei();
}
if (auto_inc_dec_ct > AUTO_COUNTER )
{
cli();
auto_inc_dec_ct = 0;
sei();
if (percent > 0 )
{
cli();
percent--;
percent_value = NEW_VALUE;
sei();
}
}
}
// If mode ON and ON_OFF button pushed, mode changes to OFF
if (( mode == ON) & (button_on_off_status == SHORT))
{
mode = OFF ;
button_on_off_status = HANDLED ;
}
// If mode OFF and ON_OFF button pushed, mode changes to ON
if (( mode == OFF) & (button_on_off_status == SHORT))
{
mode = ON ;
button_on_off_status = HANDLED ;
percent_value = NEW_VALUE;
}
// If mode ON and both PLUS and MINUS buttons pushed , mode changes to STANDBY
if (( mode == ON) & (button_plus_status == SHORT) & (button_minus_status == SHORT))
{
mode = STANDBY ;
button_plus_status = HANDLED ;
button_minus_status = HANDLED ;
percent_value = NEW_VALUE;
}
// If mode STANBY and ON_OFF button pushed, mode changes to OFF
if (( mode == STANDBY) & (button_on_off_status == SHORT))
{
mode = OFF ;
button_on_off_status = HANDLED ;
}
// If mode STANBY and both PLUS and MINUS buttons pushed , mode changes to ON
if (( mode == STANDBY) & (button_plus_status == SHORT) & (button_minus_status == SHORT))
{
mode = ON ;
button_plus_status = HANDLED ;
button_minus_status = HANDLED ;
percent_value = NEW_VALUE;
}
// switch to OFF mode if STANDBY mode lasts more then oFF_TIME minutes
// or if soldering iron is on holder more then OFF_TIME minutes
if ( (mode == STANDBY) | (holder_status == DOWN))
{
if (off_counter < OFF_TIME_COUNTER)
off_counter++;
else
mode = OFF ;
}
else
off_counter = 0;
}
//*****************************************************************************************
int main(void)
{
init_devices();
//insert your functional code here...
percent_value = NEW_VALUE ; //
cli();
percent = eeprom_read_byte (0); // after eset, read percent value from EEPROM
if(percent> 100)
percent = 100;
sei();
//eeprom_write_byte(0,10);
while(1)
{
if (!(bit_get(ACSR,BIT(ACO)))) //
{ // bit ACO not set - normal operation
if (mode==POWER_OFF) // After return from POWER_OFF mode switch to OFF mode.
{ mode = OFF; // and enable disabled interrupts
sei();
}
if (buttons_port_status == READED )
{
check_buttons (buttons_port) ;
status_handling();
buttons_port_status = NOT_READED ;
}
if ( !(mode==OFF) & !(mode==ON) & !(mode==STANDBY)) // if undefined mode, switch to OFF
mode = OFF ;
}
else // bit ACO set - everything OFF, save percent to EEPROM
{
cli(); // disable interrupts
mode = POWER_OFF ; // indicator OFF, triac OFF,
bit_clear(PORTC, BIT(TRIAC));
PORTB = numbers[10] ; // all segments OFF
bit_clear(PORTD, BIT(Q1) | BIT(Q2) | BIT(Q3)); // Q1,Q2,Q3 OFF
temp1 = eeprom_read_byte (0);
if (!(temp1 == percent)) // if current percent value different from stored in EEPROM
eeprom_write_byte(0,percent); // write percent value to EEPROM
}
}
return (0);
}
|
|
|
|
|
|
|
|
Prasymas Pas tuos kad programuoja su C |
Parašytas: 2005 05 31, 00:15 |
|
|
|
Imk dar:
Kodas: |
//ICC-AVR application builder : 2004.06.16 18:40:51
// Target : M8
// Crystal: 1.0000Mhz
#include <iom8v.h>
#include <macros.h>
#define DEBOUNCE_CTR 3
#define EEPROM_pakartojimai 6
#define LONG_PUSH_CTR 300
// 5 sekundes
// Button status ===================================
#define NOPUSH 0
#define PUSHED 1
#define SHORT 2
#define HANDLED 3
#define LONG 4
void irasyti_duomenis(unsigned int ,unsigned char ); // +
unsigned char EEPROM_nuskaitymas (unsigned int ); // +
void USART_Transmit( unsigned char ); // +
void tikrinam_knopkes ( unsigned char ,unsigned char ,unsigned char p);
void duomenu_ismetimas_i_PC (void);
void irasyti_mygtuko_koda ( unsigned char);
void irasymas_jei_paspausta (void);
unsigned char Nera_duomenu[15] = { 'N','e','r','a',' ','d','u','o','m','e','n','u',' ','!',' '};
unsigned char B_PORTAS ;
unsigned char C_PORTAS ;
unsigned char D_PORTAS ;
unsigned int sk ;
volatile unsigned char WDT_resetinimui = 0 ;
unsigned char data_debug = 0;
unsigned int j_debug = 0 ;
unsigned char EEPROM_W_skaitiklis;
unsigned char EEPROM_R_skaitiklis;
unsigned char button_flag_1 = NOPUSH; // current button status
unsigned int button_timer_1 = 0; // for debouncing button
unsigned char button_flag_2 = NOPUSH; // current button status
unsigned int button_timer_2 = 0; // for debouncing button
unsigned char button_flag_3 = NOPUSH; // current button status
unsigned int button_timer_3 = 0; // for debouncing button
unsigned char button_flag_4 = NOPUSH; // current button status
unsigned int button_timer_4 = 0; // for debouncing button
unsigned char button_flag_5 = NOPUSH; // current button status
unsigned int button_timer_5 = 0; // for debouncing button
unsigned char button_flag_6 = NOPUSH; // current button status
unsigned int button_timer_6 = 0; // for debouncing button
unsigned char button_flag_7 = NOPUSH; // current button status
unsigned int button_timer_7 = 0; // for debouncing button
unsigned char button_flag_8 = NOPUSH; // current button status
unsigned int button_timer_8 = 0; // for debouncing button
unsigned char button_flag_9 = NOPUSH; // current button status
unsigned int button_timer_9 = 0; // for debouncing button
unsigned char button_flag_10 = NOPUSH; // current button status
unsigned int button_timer_10 = 0; // for debouncing button
unsigned char button_flag_11 = NOPUSH; // current button status
unsigned int button_timer_11 = 0; // for debouncing button
unsigned char button_flag_12 = NOPUSH; // current button status
unsigned int button_timer_12 = 0; // for debouncing button
unsigned char button_flag_13 = NOPUSH; // current button status
unsigned int button_timer_13 = 0; // for debouncing button
unsigned char button_flag_14 = NOPUSH; // current button status
unsigned int button_timer_14 = 0; // for debouncing button
void port_init(void)
{
PORTB = 0xFF;
DDRB = 0x00;
PORTC = 0x7F; //m103 output only
DDRC = 0x00;
PORTD = 0xFF;
DDRD = 0x00;
}
//Watchdog initialize
// prescale: 1024K
void watchdog_init(void)
{
WDR(); //this prevents a timout on enabling
//Write logical one to WDCE and WDE
WDTCR |= (1<<WDCE) | (1<<WDE);
WDTCR = 0x0E; //WATCHDOG ENABLED - dont forget to issue WDRs
}
//TIMER2 initialize - prescale:8
// WGM: Normal
// desired value: 15mSec
// actual value: 14,893mSec (0,7%)
void timer2_init(void)
{
TCCR2 = 0x00; //stop
TIMSK &=~((1<<TOIE2)|(1<<OCIE2)); //Disable TC0 interrupt
ASSR = 0x08; //set async mode
TCNT2 = 0xC3; //setup
OCR2 = 0x3D;
TCCR2 = 0x02; //start
// Wait for TCN2UB, OCR2UB and TCR2UB
while (ASSR & BIT(TCR2UB)) //wait til loaded
;
while (ASSR & BIT(OCR2UB)) //wait til loaded
;
while (ASSR & BIT(TCN2UB)) //wait til loaded
;
//******************************************
//Clear Timer/Counter2 interrupt flags
TIFR &=~((1<<TOV2)|(1<<OCF2));
//*****************************************
}
//TWI initialize
// bit rate:20
void twi_init(void)
{
TWCR= 0X00; //disable twi
TWBR= 0x14; //set bit rate
TWSR= 0x00; //set prescale
TWAR= 0x00; //set slave address
TWCR= 0x04; //enable twi
}
//UART0 initialize
// desired baud rate: 4800
// actual: baud rate:4808 (0,2%)
// char size: 8 bit
// parity: Disabled
void uart0_init(void)
{
UCSRB = 0x00; //disable while setting baud rate
UCSRA = 0x00;
UCSRC = 0x86;
UBRRL = 0x0C; //set baud rate lo
UBRRH = 0x00; //set baud rate hi
UCSRB = 0x08;
}
//call this routine to initialize all peripherals
void init_devices(void)
{int temp0 ;
int temp1;
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
for(temp0=0;temp0<0x0180;temp0++) // Wait for external clock crystal to stabilize
{
for(temp1=0;temp1<0x0180;temp1++);
}
watchdog_init();
timer2_init();
twi_init();
uart0_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x40; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//******************************************************************************
void irasyti_mygtuko_koda ( unsigned char kodas)
{
unsigned int EEPROM_adresas = 0 ;
unsigned char EEPROM_adresas_MSB = EEPROM_nuskaitymas (0);
unsigned char EEPROM_adresas_LSB = EEPROM_nuskaitymas (1);
EEPROM_adresas = (EEPROM_adresas_MSB << 8| EEPROM_adresas_LSB);
// EEPROM_adresas = ((EEPROM_adresas_MSB << 7)& 0x07)|EEPROM_adresas_LSB ;
// Perstumiu 7 o ne 8 todel, kad 3 reikalingi MSB bitai yra pozicijose
// 3 2 1 o ne 2 1 0 .
if (EEPROM_adresas < 2047)
{
EEPROM_adresas_MSB = 0 ;
EEPROM_adresas_LSB = 0 ;
irasyti_duomenis(EEPROM_adresas ,kodas );
EEPROM_adresas++;
EEPROM_adresas_LSB = EEPROM_adresas & 0xFF ;
//EEPROM_adresas_MSB = EEPROM_adresas >> 7 ;
EEPROM_adresas_MSB = EEPROM_adresas >> 8 ;
irasyti_duomenis(0 ,EEPROM_adresas_MSB );
irasyti_duomenis(1 ,EEPROM_adresas_LSB );
}
}
//******************************************************************************
//******************************************************************************
void USART_Transmit( unsigned char data )
{
/* Wait for empty transmit buffer */
while ( !( UCSRA & (1<<UDRE)) )
;
/* Put data into buffer, sends the data */
UDR = data;
}
//******************************************************************************
//******************************************************************************
void tikrinam_knopkes ( unsigned char portas_b,unsigned char portas_c,unsigned char portas_d)
{ WDT_resetinimui++;
// PC0 - '1'
if ((portas_c & 0x01) == 0) // check bit 0 // button is down ...
{
if (button_flag_1 == NOPUSH) // is this a new push ?
{
button_timer_1 = 0; // yup - start the debounce timer
button_flag_1 = PUSHED; // note that it's down
}
if (button_flag_1 == PUSHED) // button is down - have we already noted that ?
{
button_timer_1++; // yup - inc the debounce timer
}
}
else
{
if (button_flag_1 == PUSHED)
{ // button is up, was it pushed before ?
if (button_timer_1 > DEBOUNCE_CTR)
{ // yup, was it long enough for a short push ?
button_flag_1 = SHORT; // past bounce timeout, was a short push
}
else button_flag_1 = NOPUSH; // nope, too short - was a bounce
}
if (button_flag_1 == HANDLED)
{ // button is up, did we handle it ?
button_flag_1 = NOPUSH; // yup - reset back to waiting for push
} // otherwise, wait till we handle it
}
// PC1 - '2'
if ((portas_c & 0x02) == 0) // check bit 0 // button is down ...
{
if (button_flag_2 == NOPUSH) // is this a new push ?
{
button_timer_2 = 0; // yup - start the debounce timer
button_flag_2 = PUSHED; // note that it's down
}
if (button_flag_2 == PUSHED) // button is down - have we already noted that ?
{
button_timer_2++; // yup - inc the debounce timer
}
}
else
{
if (button_flag_2 == PUSHED)
{ // button is up, was it pushed before ?
if (button_timer_2 > DEBOUNCE_CTR)
{ // yup, was it long enough for a short push ?
button_flag_2 = SHORT; // past bounce timeout, was a short push
}
else button_flag_2 = NOPUSH; // nope, too short - was a bounce
}
if (button_flag_2 == HANDLED)
{ // button is up, did we handle it ?
button_flag_2 = NOPUSH; // yup - reset back to waiting for push
} // otherwise, wait till we handle it
}
// PC2 - '3'
if ((portas_c & 0x04) == 0) // check bit 0 // button is down ...
{
if (button_flag_3 == NOPUSH) // is this a new push ?
{
button_timer_3 = 0; // yup - start the debounce timer
button_flag_3 = PUSHED; // note that it's down
}
if (button_flag_3 == PUSHED) // button is down - have we already noted that ?
{
button_timer_3++; // yup - inc the debounce timer
}
}
else
{
if (button_flag_3 == PUSHED)
{ // button is up, was it pushed before ?
if (button_timer_3 > DEBOUNCE_CTR)
{ // yup, was it long enough for a short push ?
button_flag_3 = SHORT; // past bounce timeout, was a short push
}
else button_flag_3 = NOPUSH; // nope, too short - was a bounce
}
if (button_flag_3 == HANDLED)
{ // button is up, did we handle it ?
button_flag_3 = NOPUSH; // yup - reset back to waiting for push
} // otherwise, wait till we handle it
}
// PC3 - '4'
if ((portas_c & 0x08) == 0) // check bit 0 // button is down ...
{
if (button_flag_4 == NOPUSH) // is this a new push ?
{
button_timer_4 = 0; // yup - start the debounce timer
button_flag_4 = PUSHED; // note that it's down
}
if (button_flag_4 == PUSHED) // button is down - have we already noted that ?
{
button_timer_4++; // yup - inc the debounce timer
}
}
else
{
if (button_flag_4 == PUSHED)
{ // button is up, was it pushed before ?
if (button_timer_4 > DEBOUNCE_CTR)
{ // yup, was it long enough for a short push ?
button_flag_4 = SHORT; // past bounce timeout, was a short push
}
else button_flag_4 = NOPUSH; // nope, too short - was a bounce
}
if (button_flag_4 == HANDLED)
{ // button is up, did we handle it ?
button_flag_4 = NOPUSH; // yup - reset back to waiting for push
} // otherwise, wait till we handle it
}
// PD6 - '5'
if ((portas_d & 0x40) == 0) // check bit 0 // button is down ...
{
if (button_flag_5 == NOPUSH) // is this a new push ?
{
button_timer_5 = 0; // yup - start the debounce timer
button_flag_5 = PUSHED; // note that it's down
}
if (button_flag_5 == PUSHED) // button is down - have we already noted that ?
{
button_timer_5++; // yup - inc the debounce timer
}
}
else
{
if (button_flag_5 == PUSHED)
{ // button is up, was it pushed before ?
if (button_timer_5 > DEBOUNCE_CTR)
{ // yup, was it long enough for a short push ?
button_flag_5 = SHORT; // past bounce timeout, was a short push
}
else button_flag_5 = NOPUSH; // nope, too short - was a bounce
}
if (button_flag_5 == HANDLED)
{ // button is up, did we handle it ?
button_flag_5 = NOPUSH; // yup - reset back to waiting for push
} // otherwise, wait till we handle it
}
// PD7 - '6'
if ((portas_d & 0x80) == 0) // check bit 0 // button is down ...
{
if (button_flag_6 == NOPUSH) // is this a new push ?
{
button_timer_6 = 0; // yup - start the debounce timer
button_flag_6 = PUSHED; // note that it's down
}
if (button_flag_6 == PUSHED) // button is down - have we already noted that ?
{
button_timer_6++; // yup - inc the debounce timer
}
}
else
{
if (button_flag_6 == PUSHED)
{ // button is up, was it pushed before ?
if (button_timer_6 > DEBOUNCE_CTR)
{ // yup, was it long enough for a short push ?
button_flag_6 = SHORT; // past bounce timeout, was a short push
}
else button_flag_6 = NOPUSH; // nope, too short - was a bounce
}
if (button_flag_6 == HANDLED)
{ // button is up, did we handle it ?
button_flag_6 = NOPUSH; // yup - reset back to waiting for push
} // otherwise, wait till we handle it
}
// PB0 - '7'
if ((portas_b & 0x01) == 0) // check bit 0 // button is down ...
{
if (button_flag_7 == NOPUSH) // is this a new push ?
{
button_timer_7 = 0; // yup - start the debounce timer
button_flag_7 = PUSHED; // note that it's down
}
if (button_flag_7 == PUSHED) // button is down - have we already noted that ?
{
button_timer_7++; // yup - inc the debounce timer
}
}
else
{
if (button_flag_7 == PUSHED)
{ // button is up, was it pushed before ?
if (button_timer_7 > DEBOUNCE_CTR)
{ // yup, was it long enough for a short push ?
button_flag_7 = SHORT; // past bounce timeout, was a short push
}
else button_flag_7 = NOPUSH; // nope, too short - was a bounce
}
if (button_flag_7 == HANDLED)
{ // button is up, did we handle it ?
button_flag_7 = NOPUSH; // yup - reset back to waiting for push
} // otherwise, wait till we handle it
}
// PB1 - '8'
if ((portas_b & 0x02) == 0) // check bit 0 // button is down ...
{
if (button_flag_8 == NOPUSH) // is this a new push ?
{
button_timer_8 = 0; // yup - start the debounce timer
button_flag_8 = PUSHED; // note that it's down
}
if (button_flag_8 == PUSHED) // button is down - have we already noted that ?
{
button_timer_8++; // yup - inc the debounce timer
}
}
else
{
if (button_flag_8 == PUSHED)
{ // button is up, was it pushed before ?
if (button_timer_8 > DEBOUNCE_CTR)
{ // yup, was it long enough for a short push ?
button_flag_8 = SHORT; // past bounce timeout, was a short push
}
else button_flag_8 = NOPUSH; // nope, too short - was a bounce
}
if (button_flag_8 == HANDLED)
{ // button is up, did we handle it ?
button_flag_8 = NOPUSH; // yup - reset back to waiting for push
} // otherwise, wait till we handle it
}
// PB2 - '9'
if ((portas_b & 0x04) == 0) // check bit 0 // button is down ...
{
if (button_flag_9 == NOPUSH) // is this a new push ?
{
button_timer_9 = 0; // yup - start the debounce timer
button_flag_9 = PUSHED; // note that it's down
}
if (button_flag_9 == PUSHED) // button is down - have we already noted that ?
{
button_timer_9++; // yup - inc the debounce timer
}
}
else
{
if (button_flag_9 == PUSHED)
{ // button is up, was it pushed before ?
if (button_timer_9 > DEBOUNCE_CTR)
{ // yup, was it long enough for a short push ?
button_flag_9 = SHORT; // past bounce timeout, was a short push
}
else button_flag_9 = NOPUSH; // nope, too short - was a bounce
}
if (button_flag_9 == HANDLED)
{ // button is up, did we handle it ?
button_flag_9 = NOPUSH; // yup - reset back to waiting for push
} // otherwise, wait till we handle it
}
// PB3 - 'A'
if ((portas_b & 0x08) == 0) // check bit 0 // button is down ...
{
if (button_flag_10 == NOPUSH) // is this a new push ?
{
button_timer_10 = 0; // yup - start the debounce timer
button_flag_10 = PUSHED; // note that it's down
}
if (button_flag_10 == PUSHED) // button is down - have we already noted that ?
{
button_timer_10++; // yup - inc the debounce timer
}
}
else
{
if (button_flag_10 == PUSHED)
{ // button is up, was it pushed before ?
if (button_timer_10 > DEBOUNCE_CTR)
{ // yup, was it long enough for a short push ?
button_flag_10 = SHORT; // past bounce timeout, was a short push
}
else button_flag_10 = NOPUSH; // nope, too short - was a bounce
}
if (button_flag_10 == HANDLED)
{ // button is up, did we handle it ?
button_flag_10 = NOPUSH; // yup - reset back to waiting for push
} // otherwise, wait till we handle it
}
// PB4 - 'B'
if ((portas_b & 0x10) == 0) // check bit 0 // button is down ...
{
if (button_flag_11 == NOPUSH) // is this a new push ?
{
button_timer_11 = 0; // yup - start the debounce timer
button_flag_11 = PUSHED; // note that it's down
}
if (button_flag_11 == PUSHED) // button is down - have we already noted that ?
{
button_timer_11++; // yup - inc the debounce timer
}
}
else
{
if (button_flag_11 == PUSHED)
{ // button is up, was it pushed before ?
if (button_timer_11 > DEBOUNCE_CTR)
{ // yup, was it long enough for a short push ?
button_flag_11 = SHORT; // past bounce timeout, was a short push
}
else button_flag_11 = NOPUSH; // nope, too short - was a bounce
}
if (button_flag_11 == HANDLED)
{ // button is up, did we handle it ?
button_flag_11 = NOPUSH; // yup - reset back to waiting for push
} // otherwise, wait till we handle it
}
// PB5 - 'C'
if ((portas_b & 0x20) == 0) // check bit 0 // button is down ...
{
if (button_flag_12 == NOPUSH) // is this a new push ?
{
button_timer_12 = 0; // yup - start the debounce timer
button_flag_12 = PUSHED; // note that it's down
}
if (button_flag_12 == PUSHED) // button is down - have we already noted that ?
{
button_timer_12++; // yup - inc the debounce timer
}
}
else
{
if (button_flag_12 == PUSHED)
{ // button is up, was it pushed before ?
if (button_timer_12 > DEBOUNCE_CTR)
{ // yup, was it long enough for a short push ?
button_flag_12 = SHORT; // past bounce timeout, was a short push
}
else button_flag_12 = NOPUSH; // nope, too short - was a bounce
}
if (button_flag_12 == HANDLED)
{ // button is up, did we handle it ?
button_flag_12 = NOPUSH; // yup - reset back to waiting for push
} // otherwise, wait till we handle it
}
// Ismetimo mygtukas - PD5
if ((portas_d & 0x20) == 0) // check bit 0 // button is down ...
{
if (button_flag_13 == NOPUSH) // is this a new push ?
{
button_timer_13 = 0; // yup - start the debounce timer
button_flag_13 = PUSHED; // note that it's down
}
if (button_flag_13 == PUSHED) // button is down - have we already noted that ?
{
button_timer_13++; // yup - inc the debounce timer
if (button_timer_13 >= LONG_PUSH_CTR)
{ // long enough for a LONG push ?
button_flag_13 = LONG; // yup - note a LONG push
}
}
}
else
{
if (button_flag_13 == PUSHED)
{ // button is up, was it pushed before ?
if (button_timer_13 > DEBOUNCE_CTR)
{ // yup, was it long enough for a short push ?
button_flag_13 = SHORT; // past bounce timeout, was a short push
}
else button_flag_13 = NOPUSH; // nope, too short - was a bounce
}
if (button_flag_13 == HANDLED)
{ // button is up, did we handle it ?
button_flag_13 = NOPUSH; // yup - reset back to waiting for push
} // otherwise, wait till we handle it
}
// PD0 - debuginimui. Ismeta i porta visa EEPROM turini
if ((portas_d & 0x01) == 0) // check bit 0 // button is down ...
{
if (button_flag_14 == NOPUSH) // is this a new push ?
{
button_timer_14 = 0; // yup - start the debounce timer
button_flag_14 = PUSHED; // note that it's down
}
if (button_flag_14 == PUSHED) // button is down - have we already noted that ?
{
button_timer_14++; // yup - inc the debounce timer
}
}
else
{
if (button_flag_14 == PUSHED)
{ // button is up, was it pushed before ?
if (button_timer_14 > DEBOUNCE_CTR)
{ // yup, was it long enough for a short push ?
button_flag_14 = SHORT; // past bounce timeout, was a short push
}
else button_flag_14 = NOPUSH; // nope, too short - was a bounce
}
if (button_flag_14 == HANDLED)
{ // button is up, did we handle it ?
button_flag_14 = NOPUSH; // yup - reset back to waiting for push
} // otherwise, wait till we handle it
}
}
//******************************************************************************
//******************************************************************************
void irasymas_jei_paspausta (void)
{
if (button_flag_1 == SHORT)
{
irasyti_mygtuko_koda ( '7');
button_flag_1 = HANDLED;
}
//******************
if (button_flag_2 == SHORT)
{
irasyti_mygtuko_koda ( '8');
button_flag_2 = HANDLED;
}
//*****************
if (button_flag_3 == SHORT)
{
irasyti_mygtuko_koda ( '9');
button_flag_3 = HANDLED;
}
//*********************
if (button_flag_4 == SHORT)
{
irasyti_mygtuko_koda ( 'A');
button_flag_4 = HANDLED;
}
//**********************
if (button_flag_5 == SHORT)
{
irasyti_mygtuko_koda ( 'B');
button_flag_5 = HANDLED;
}
//**********************
if (button_flag_6 == SHORT)
{
irasyti_mygtuko_koda ( 'C');
button_flag_6 = HANDLED;
}
//***********************
if (button_flag_7 == SHORT)
{
irasyti_mygtuko_koda ( '1');
button_flag_7 = HANDLED;
}
//***********************
if (button_flag_8 == SHORT)
{
irasyti_mygtuko_koda ( '2');
button_flag_8 = HANDLED;
}
//***********************
if (button_flag_9 == SHORT)
{
irasyti_mygtuko_koda ( '3');
button_flag_9 = HANDLED;
}
//***********************
if (button_flag_10 == SHORT)
{
irasyti_mygtuko_koda ( '4');
button_flag_10 = HANDLED;
}
//***********************
if (button_flag_11 == SHORT)
{
irasyti_mygtuko_koda ( '5');
button_flag_11 = HANDLED;
}
//***********************
if (button_flag_12 == SHORT)
{
irasyti_mygtuko_koda ( '6');
button_flag_12 = HANDLED;
}
//***********************
}
//******************************************************************************
//******************************************************************************
#pragma interrupt_handler timer2_ovf_isr:5
void timer2_ovf_isr(void)
{
MCUCR &=~(1<<SE) ;
TCCR2 = 0x00; //stop
TIMSK &=~((1<<TOIE2)|(1<<OCIE2)); //Disable TC0 interrupt
// Nuskaitome portu reiksmes
B_PORTAS = PINB ;
C_PORTAS = PINC ;
D_PORTAS = PIND ;
tikrinam_knopkes ( B_PORTAS,C_PORTAS,D_PORTAS);
// USART_Transmit('A' );
WDT_resetinimui++;
}
//******************************************************************************
//******************************************************************************
void irasyti_duomenis(unsigned int EEPROM_adresas,unsigned char duomenys)
{
unsigned char EEPROM_adresas_MSB ;
unsigned char EEPROM_adresas_LSB ;
CLI();
if (EEPROM_adresas <= 2047)
{
EEPROM_adresas_LSB = 0;
EEPROM_adresas_MSB = 0;
EEPROM_W_skaitiklis = 0 ;
EEPROM_adresas_LSB = EEPROM_adresas & 0xFF;
EEPROM_adresas_MSB = EEPROM_adresas >> 7;
//MSB perstumiu 7 o ne 8 todel, kad 3 adreso MSB bitai
//turi buti pozicijose 3 2 1, o ne 2 1 0
Rasymas:
if (EEPROM_W_skaitiklis < EEPROM_pakartojimai)
{
EEPROM_W_skaitiklis++;
TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN) ;//Send START condition
while (!(TWCR & (1<<TWINT))) ; //Wait for TWINT Flag set. This
; //indicates that the START condition has been transmitted
if ( ((TWSR & 0xF8) != 0x08)&((TWSR & 0xF8) != 0x10) )
goto Rasymas ; // Check value of TWI Status
//Register. Mask prescaler bits. If
//status different from START go to
//Load SLA_W into TWDR Register.
//Clear TWINT bit in TWCR to start
//transmission of address
TWDR = EEPROM_adresas_MSB & 0x0E | 0xA0 ;
TWCR = (1<<TWINT) | (1<<TWEN);
//Wait for TWINT Flag set. This
//indicates that the SLA+W has been
//transmitted, and ACK/NACK has
//been received.
while (!(TWCR & (1<<TWINT)))
;
//Check value of TWI Status
//Register. Mask prescaler bits. If
//status different from MT_SLA_ACK
//go to ERROR
if ((TWSR & 0xF8) != 0x18)
goto Rasymas ;
//Load DATA into TWDR Register.
//Clear TWINT bit in TWCR to start
//transmission of data
TWDR = EEPROM_adresas_LSB;
TWCR = (1<<TWINT) | (1<<TWEN);
//Wait for TWINT Flag set. This
//indicates that the DATA has been
//transmitted, and ACK/NACK has
//been received.
while (!(TWCR & (1<<TWINT)))
;
//Check value of TWI Status
//Register. Mask prescaler bits. If
//status different from
//MT_DATA_ACK go to ERROR
if ((TWSR & 0xF8) !=0x28)
goto Rasymas;
//Load DATA into TWDR Register.
//Clear TWINT bit in TWCR to start
//transmission of data
TWDR = duomenys;
TWCR = (1<<TWINT) | (1<<TWEN);
//Wait for TWINT Flag set. This
//indicates that the DATA has been
//transmitted, and ACK/NACK has
//been received.
while (!(TWCR & (1<<TWINT)))
;
//Check value of TWI Status
//Register. Mask prescaler bits. If
//status different from
//MT_DATA_ACK go to ERROR
if ((TWSR & 0xF8) !=0x28)
goto Rasymas;
//Transmit STOP condition
TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
// palaukiam kol iraso duomenis
/* Laukiam:
TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN) ;//Send START condition
while (!(TWCR & (1<<TWINT))) ; //Wait for TWINT Flag set. This
; //indicates that the START condition has been transmitted
if ( ((TWSR & 0xF8) != 0x08)&((TWSR & 0xF8) != 0x10) )
goto Laukiam ; // Check value of TWI Status
//Register. Mask prescaler bits. If
//status different from START go to
//Load SLA_W into TWDR Register.
//Clear TWINT bit in TWCR to start
//transmission of address
TWDR = EEPROM_adresas_MSB & 0x0E | 0xA0 ;
//debug
//USART_Transmit( TWDR) ;
//
TWCR = (1<<TWINT) | (1<<TWEN);
//Wait for TWINT Flag set. This
//indicates that the SLA+W has been
//transmitted, and ACK/NACK has
//been received.
while (!(TWCR & (1<<TWINT)))
;
//Check value of TWI Status
//Register. Mask prescaler bits. If
//status different from MT_SLA_ACK
//go to ERROR
if ((TWSR & 0xF8) != 0x18)
goto Laukiam ;
//Transmit STOP condition
TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
*/
}
}
SEI();
}
//******************************************************************************
//******************************************************************************
unsigned char EEPROM_nuskaitymas (unsigned int EEPROM_adresas)
{ unsigned char data = 0 ;
unsigned char EEPROM_adresas_MSB ;
unsigned char EEPROM_adresas_LSB ;
if (EEPROM_adresas <= 2047)
{
EEPROM_R_skaitiklis = 0 ;
EEPROM_adresas_LSB = 0;
EEPROM_adresas_MSB = 0;
EEPROM_adresas_LSB = EEPROM_adresas & 0xFF;
EEPROM_adresas_MSB = EEPROM_adresas >>7;
//MSB perstumiu 7 o ne 8 todel, kad 3 adreso MSB bitai
//turi buti pozicijose 3 2 1, o ne 2 1 0
Nuskaitymas:
if (EEPROM_R_skaitiklis < EEPROM_pakartojimai)
{
EEPROM_R_skaitiklis++;
TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN) ;//Send START condition
while (!(TWCR & (1<<TWINT))) ; //Wait for TWINT Flag set. This
; //indicates that the START condition has been transmitted
if ( ((TWSR & 0xF8) != 0x08)&((TWSR & 0xF8) != 0x10) )
goto Nuskaitymas ; // Check value of TWI Status
//Register. Mask prescaler bits. If
//status different from START go to
//Load SLA_W into TWDR Register.
//Clear TWINT bit in TWCR to start
//transmission of address
TWDR = EEPROM_adresas_MSB & 0x0E | 0xA0 ;
TWCR = (1<<TWINT) | (1<<TWEN);
//Wait for TWINT Flag set. This
//indicates that the SLA+W has been
//transmitted, and ACK/NACK has
//been received.
while (!(TWCR & (1<<TWINT)))
;
//Check value of TWI Status
//Register. Mask prescaler bits. If
//status different from MT_SLA_ACK
//go to ERROR
if ((TWSR & 0xF8) != 0x18)
goto Nuskaitymas ;
//Load DATA into TWDR Register.
//Clear TWINT bit in TWCR to start
//transmission of data
TWDR = EEPROM_adresas_LSB;
TWCR = (1<<TWINT) | (1<<TWEN);
//Wait for TWINT Flag set. This
//indicates that the DATA has been
//transmitted, and ACK/NACK has
//been received.
while (!(TWCR & (1<<TWINT)))
;
//Check value of TWI Status
//Register. Mask prescaler bits. If
//status different from
//MT_DATA_ACK go to ERROR
if ((TWSR & 0xF8) !=0x28)
goto Nuskaitymas ;
// tipo adresas jau nurodytas, reikia pasiusti skaitymo komanda ir gausi
// duomenis
TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN) ;//Send START condition
while (!(TWCR & (1<<TWINT))) ; //Wait for TWINT Flag set. This
; //indicates that the START condition has been transmitted
if ( ((TWSR & 0xF8) != 0x08)&((TWSR & 0xF8) != 0x10) )
goto Nuskaitymas ; // Check value of TWI Status
//Register. Mask prescaler bits. If
//status different from START go to
//Load SLA_R into TWDR Register.
//Clear TWINT bit in TWCR to start
//transmission of address
TWDR = EEPROM_adresas_MSB & 0x0E | 0xA1 ;
TWCR = (1<<TWINT) | (1<<TWEN);
//Wait for TWINT Flag set. This
//indicates that the SLA+R has been
//transmitted, and ACK/NACK has
//been received.
while (!(TWCR & (1<<TWINT)))
;
if ((TWSR & 0xF8) != 0x40)
goto Nuskaitymas ;
// dabar reikia ismesti tik (1<<TWINT) | (1<<TWEN);
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)))
;
if ((TWSR & 0xF8) != 0x58)
goto Nuskaitymas ;
data = TWDR ;
//Transmit STOP condition
TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
}
}
return data;
}
//******************************************************************************
//******************************************************************************
void duomenu_ismetimas_i_PC (void)
{
unsigned int EEPROM_adresas = 0;
unsigned char EEPROM_adresas_MSB ;
unsigned char EEPROM_adresas_LSB ;
unsigned char data = 0;
unsigned char i = 0 ;
unsigned int j = 0 ;
EEPROM_adresas_MSB = EEPROM_nuskaitymas (0);
EEPROM_adresas_LSB = EEPROM_nuskaitymas (1);
//EEPROM_adresas = ((EEPROM_adresas_MSB << 7)& 0x07)|EEPROM_adresas_LSB ;
EEPROM_adresas = (EEPROM_adresas_MSB << 8| EEPROM_adresas_LSB);
if (EEPROM_adresas < 3)
{
for (i = 0; i<15 ; i++)
USART_Transmit( Nera_duomenu[i] );
}
else
{
for (j = 2; j < EEPROM_adresas ; j++)
{
data = EEPROM_nuskaitymas (j);
USART_Transmit( data );
WDR();
}
}
}
//******************************************************************************
void main(void)
{
init_devices();
B_PORTAS = 0xFF ;
C_PORTAS = 0xFF ;
D_PORTAS = 0xFF ;
//insert your functional code here...
while(1)
{
TCNT2 = 0xC3; //setup
while ((ASSR & (1<<TCN2UB)))
;
TCCR2 = 0x02; //start
while (ASSR & BIT(TCR2UB)) //wait til loaded
;
TIMSK = 0x40; //timer interrupt sources
MCUCR = 0xB0; //entering sleeping mode: power save mode
asm("sleep");
asm("nop");
//MCUCR &=~(1<<SE) ;
//TCCR2 = 0x00; //stop
//TIMSK &=~((1<<TOIE2)|(1<<OCIE2)); //Disable TC0 interrupt
if (WDT_resetinimui == 4)
{
WDR();
WDT_resetinimui = 0 ;
}
if (button_flag_14 == SHORT)
{
for (sk = 0 ;sk < 0x0FFF ; sk++)
;
for (j_debug = 0; j_debug < 2047 ; j_debug++)
{
data_debug = EEPROM_nuskaitymas (j_debug);
USART_Transmit( data_debug );
// USART_Transmit('D' );
WDR();
}
button_flag_14 = NOPUSH;
for (sk = 0 ;sk < 0x00FF ; sk++)
;
}
if (button_flag_13 == SHORT)
{
for (sk = 0 ;sk < 0x0FFF ; sk++)
;
duomenu_ismetimas_i_PC ();
button_flag_13 = HANDLED;
for (sk = 0 ;sk < 0x00FF ; sk++)
;
}
else
{
if (button_flag_13 == LONG)
{
irasyti_duomenis(0 ,0x00 );
irasyti_duomenis(1 ,0x02 );
button_flag_13 = NOPUSH;
}
else
{
irasymas_jei_paspausta ();
}
}
}
}
|
|
|
|
|
|
|
|
Prasymas Pas tuos kad programuoja su C |
Parašytas: 2009 10 21, 17:57 |
|
|
|
Ech.. toks vaizdas kad tik ziurima kas ilgesni koda papastins... Moderas budamas ispejimus gautu tokie.. jei jau ilgas kodas tam yra visokie pastebin...
o zmogui del mobiliako manau apie UART reik kodo visgi tie telefai kiek zinau taip ir bendrauja (TX,RX).. |
|
|
|
|
|
|
Prasymas Pas tuos kad programuoja su C |
Parašytas: 2009 10 22, 07:42 |
|
|
|
taigi yra tas kodas... UART. Va:
//******************************************************************************
void USART_Transmit( unsigned char data )
{
/* Wait for empty transmit buffer */
while ( !( UCSRA & (1<<UDRE)) )
;
/* Put data into buffer, sends the data */
UDR = data;
}
//******************************************************************************
Parasai programoje :USART_Transmit( data ) ir siuncia...
Kai kas isivaizduoja, kad uztenka parasyti keleta eiluciu ir duomenis yra siunciami... kad parasyti net nedidele programele kontroleriui butina suprasti jo veikimo principus, struktura ir moketi ja sukonfiguruoti ir suvaldyti... |
|
_________________ Nieko taip lengvai nedaliname, kaip patarimus... |
|
|
|
|
|
Prasymas Pas tuos kad programuoja su C |
Parašytas: 2009 10 22, 13:12 |
|
|
|
jei tu resusrsu einamu momentu niekam nereikia, tai ar gaila tu resursu? Viskas cia OK. |
|
_________________ Nieko taip lengvai nedaliname, kaip patarimus... |
|
|
|
|
|
Google paieška forume |
|
|
Naujos temos forume |
|
|
FS25 Tractors
Farming Simulator 25 Mods,
FS25 Maps,
FS25 Trucks |
|
ETS2 Mods
ETS2 Trucks,
ETS2 Bus,
Euro Truck Simulator 2 Mods
|
|
FS22 Tractors
Farming Simulator 22 Mods,
FS22 Maps,
FS25 Mods |
|
VAT calculator
VAT number check,
What is VAT,
How much is VAT |
|
LEGO
Mänguköök,
mudelautod,
nukuvanker |
|
Thermal monocular
Thermal vision camera,
Night vision ar scope,
Night vision spotting scope |
|
FS25 Mods
FS25 Harvesters,
FS25 Tractors Mods,
FS25 Maps Mods |
|
Dantų protezavimas
All on 4 implantai,
Endodontija mikroskopu,
Dantų implantacija |
|
FS25 Mods
FS25 Maps,
FS25 Cheats,
FS25 Install Mods |
|
GTA 6 Weapons
GTA 6 Characters,
GTA 6 Map,
GTA 6 Vehicles |
|
FS25 Mods
Farming Simulator 25 Mods,
FS25 Maps |
|
|
|