//#include "suryaa.h"

void pin_configure(void);
void transmit_byte_serially(uint8_t);
void output1(char);
void transmit_string(const char*);
void start_timer_baud_10000(void);
void wait_&_stop_timer(void);



void pin_configure(void)
{
  volatile char*dirf,*outf;
  dirf=(volatile char*)0x30;
  outf=(volatile char*)0x31;
  *dirf=0xFF;
  *outf=0x01;
}

void output1(char x)
{
  volatile char*outf=0x31;
  *outf=x;
}



void transmit_byte_serially(uint8_t x)
{
  start_timer_baud_10000();
  output1(0x00);//start bit;
  wait_&_stop_timer();
  for(i=0;i<8;i++)//8 data bits send serially after specified time.
 {
  start_timer_baud_10000();
   if (x & (1 << i))
   {
    output1(0x01);}
    else{
    output1(0x00);}
    wait_&_stop_timer();
 }
  start_timer_baud_10000();
  output1(0x01); //stop bit()
  wait_&_stop_timer();
}

void transmit_string(const char* str) 
{
    while (*str)
     {
        transmit_byte_serially(*str);
        *str++;
    }
}

void start_timer_baud_10000(void)
{
  volatile char*tccr1a,*tccr1b,*ocr1aL,*ocr1aL,*ocr1aH;
  tccr1a=(volatile char*)0x80;
  tccr1b=(volatile char*)0x81;
  ocr1aL=(volatile char*)0x88;
  ocr1aH=(volatile char*)0x89;

  *tccr1a=0x00;
  *tccr1b=0x0A;//ctc mode,prescalr 8 so 0.5usdelay per cycle
  *ocr1aH=0x00;
  *0cr1aL=0xc7; //load value 199 to compare register for exact 100us delay


}

void wait_&_stop_timer(void)
{
  while(1)
  {
    volatile char*tifr1,*tcnt1L,*tcnt1H,*tccr1b;
    tifr1=(volatile chafr*)0x36;
    tcnt1L=(volatile char*)0x84;
    tcnt1H=(volatile char*)0x85;
    tccr1b=(volatile char*)0x81;
    if(*tifr1==0x02)//see if second bit in flag register is set
     {
      *tifr1=0x00;
      *tcnt1H=0x00;
      *tcnt1L=0x00;
      *tccr1b==0x00;//stop timer
       break;
     }
  }
}


void setup() 
{
  pin_configure();
}

void loop()
{
  volatile long i;
  transmit_string("hello world");
  for(i=0;i<200000;i++);
}











/*#include <avr/io.h>
#include <util/delay.h>

#define F_CPU 16000000UL  // MCU operating frequency
#define BAUD_RATE 9600    // Desired baud rate

#define RX_PIN   PB0      // Receive pin (for UART RX)
#define RX_DDR   DDRB     // Data Direction Register for RX pin
#define RX_PORT  PORTB    // Port Register for RX pin
#define RX_PIN_REG PINB   // Input Register for RX pin

#define BIT_DELAY_US (1000000 / BAUD_RATE) // Bit delay in microseconds

void uart_init(void) {
    // Set RX pin as input
    RX_DDR &= ~(1 << RX_PIN);

    // Enable pull-up resistor on RX pin (optional, depends on your circuit)
    RX_PORT |= (1 << RX_PIN);
}

uint8_t uart_receive_byte(void) {
    uint8_t data = 0;

    // Wait for start bit (logic low)
    while (RX_PIN_REG & (1 << RX_PIN));

    // Wait for half bit duration to sample in the middle of the bit
    _delay_us(BIT_DELAY_US / 2);

    // Read 8 data bits (LSB first)
    for (uint8_t i = 0; i < 8; i++) {
        _delay_us(BIT_DELAY_US);  // Wait for the bit duration
        if (RX_PIN_REG & (1 << RX_PIN)) {
            data |= (1 << i);  // Set bit in data variable if RX_PIN is high
        }
    }

    // Wait for stop bit (logic high)
    _delay_us(BIT_DELAY_US);

    return data;
}

int main(void) {
    uart_init();  // Initialize UART

    while (1) {
        uint8_t received_byte = uart_receive_byte();  // Receive byte
        // Process received byte (e.g., store it, display it, etc.)
        // Here we can add code to handle the received data
    }
}*/
mega:SCL
mega:SDA
mega:AREF
mega:GND.1
mega:13
mega:12
mega:11
mega:10
mega:9
mega:8
mega:7
mega:6
mega:5
mega:4
mega:3
mega:2
mega:1
mega:0
mega:14
mega:15
mega:16
mega:17
mega:18
mega:19
mega:20
mega:21
mega:5V.1
mega:5V.2
mega:22
mega:23
mega:24
mega:25
mega:26
mega:27
mega:28
mega:29
mega:30
mega:31
mega:32
mega:33
mega:34
mega:35
mega:36
mega:37
mega:38
mega:39
mega:40
mega:41
mega:42
mega:43
mega:44
mega:45
mega:46
mega:47
mega:48
mega:49
mega:50
mega:51
mega:52
mega:53
mega:GND.4
mega:GND.5
mega:IOREF
mega:RESET
mega:3.3V
mega:5V
mega:GND.2
mega:GND.3
mega:VIN
mega:A0
mega:A1
mega:A2
mega:A3
mega:A4
mega:A5
mega:A6
mega:A7
mega:A8
mega:A9
mega:A10
mega:A11
mega:A12
mega:A13
mega:A14
mega:A15