1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

bad working mcu with _delay_ms()

Discussion in 'AVR' started by Majid soltani, May 26, 2015.

  1. Majid soltani

    Majid soltani New Member

    Joined:
    May 6, 2015
    Messages:
    22
    Likes:
    0
    hi guys

    i use timer1 for send NTSC Signal To Composite TV.

    but my problem is this :
    i want after 1 secound delay change frame buffer inormation but when i call _delay_ms(1000) , i see big noise on tv.

    even i replace _delay_ms() with a long while() loop, but i see big noise again;

    my code is :


    /*
    *
    * ATmega32a NTSC Video Signal Generator
    * CPU Frequency : 20 MHz
    *
    * ------------------------------------------------------------
    * Pin Used : PORTB0 --> Sync Signal
    * PORTB5 (MOSI) --> Video Signal
    * ------------------------------------------------------------
    *
    * Written by : Majid Soltani
    *
    * Date : 1394/01/06
    * Firmware Version : 1.1
    *
    * Tested on : JVC 15" CRT TV
    * Sony 32" LCD
    * EasyCAP USB Video Capture
    */


    #define F_CPU 20000000UL
    #include <avr/io.h>
    #include <util/delay.h>
    #include <avr/interrupt.h>
    #include "header/Pattern.h"
    #include "header/Main_Config.h"
    #include "header/delay.h"

    uint16_t rasterline=0; // Counting lines drawn on TV
    unsigned char line_row asm("line_r") = 0;
    unsigned int screen_row asm("scr_r") = 0;
    //Main Screen buffer for Drawn
    unsigned int Screen[19*28] asm("sc_b")={
    0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
    0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
    0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x080,0x000,0x000,
    0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
    0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
    0x000,0x000,0x000,0x000,0x080,0x000,0x000,0x000,0x000,0x000,0x080,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
    0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
    0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
    0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
    0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
    0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x080,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
    0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
    0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
    0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
    0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
    0x000,0x000,0x080,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
    0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
    0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
    0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
    0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
    0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
    0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
    0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
    0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
    0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
    0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x040,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
    0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,
    0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000,0x000};

    /* Video buffer */
    /* Timers and SPI initialization */
    void timer_init(void){
    line_row = 0;
    screen_row = 0;

    init_sync_port;

    TCCR1B = (1<<WGM12);
    TIMSK |= (1<<OCIE1A);
    TCNT1 = 0;
    OCR1A = 1270;
    TCCR1B |= (1<<CS10);
    }

    /* Timer interrupt */
    ISR (TIMER1_COMPA_vect) {

    if((rasterline > 31 && rasterline <= 255))
    { // ____________
    _delay_us(1.5); // Front porch ( FP ) ___| |_
    blank_sync; // ___/ |
    _delay_us(4.7); // Horizontal Black interval ( HSync ) __ __/\/\/\/\/\/\/\/\/\_| Draw Color |__
    sync; // FP| HS |BP Color Burst FP
    _delay_us(4.7); // Back porch ( BP ) ----


    //------------- Patter Draw Start -----------
    // R16 -> for read cell color for each row from PROGMEM array
    // R30 & R31 -> for set address of POGMEM array
    // R19 -> for get line_row variable value
    // R26 & R28 -> for set address of screen array
    // R17 -> value of each cell screen_row
    // R21 -> Column Counter 0-9
    asm volatile(" PUSH R16 \
    \n PUSH R18 \
    \n PUSH R17 \
    \n PUSH R30 \
    \n PUSH R31 \
    \n PUSH R19 \
    \n PUSH R20 \
    \n PUSH R26 \
    \n PUSH R27 \
    \n PUSH R21 \
    \n LDI R21,0 \
    \n LDS R19,line_r \
    \n LDI R30,lo8(scr_r) \
    \n LDI R31,hi8(scr_r) \
    \n LD R16,Z+ \
    \n LD R20,Z+ \
    \n LDI R26,lo8(sc_b) \
    \n LDI R27,hi8(sc_b) \
    \n ADD R26,R16 \
    \n ADC R27,R20 \
    \n;-------------------------------------\
    \n LD R17,X+ \
    \n LD R18,X+ \
    \n LDI R30,lo8(sc_p) \
    \n LDI R31,hi8(sc_p) \
    \n ADD R30,R19 \
    \n ADC R31,0 \
    \n ADD R30,R17 \
    \n ADC R31,R18 \
    \n;-------------------------------------\
    \n LOOP: \
    \n;-------------------------------------\
    \n LPM R16 , Z+ \
    \n OUT 0x12 , R16 \
    \n LD R17,X+ \
    \n;-------------------------------------\
    \n LPM R16 , Z+ \
    \n OUT 0x12 , R16 \
    \n LD R18,X+ \
    \n;-------------------------------------\
    \n LPM R16 , Z+ \
    \n OUT 0x12 , R16 \
    \n LDI R28,lo8(sc_p) \
    \n LDI R29,hi8(sc_p) \
    \n;-------------------------------------\
    \n LPM R16 , Z+ \
    \n OUT 0x12 , R16 \
    \n ADD R28,R19 \
    \n ADC R29,0 \
    \n;-------------------------------------\
    \n LPM R16 , Z+ \
    \n OUT 0x12 , R16 \
    \n ADD R28,R17 \
    \n;-------------------------------------\
    \n LPM R16 , Z+ \
    \n OUT 0x12 , R16 \
    \n ADC R29,R18 \
    \n;-------------------------------------\
    \n LPM R16 , Z+ \
    \n OUT 0x12 , R16 \
    \n INC R21 \
    \n;-------------------------------------\
    \n LPM R16 , Z \
    \n OUT 0x12 , R16 \
    \n MOVW R30,R28 \
    \n;-------------------------------------\
    \n CPI R21,0x13 \
    \n BRNE LOOP \
    \n OUT 0x12 , 0x0 \
    \n End: POP R21 \
    \n POP R27 \
    \n POP R26 \
    \n POP R20 \
    \n POP R19 \
    \n POP R31 \
    \n POP R30 \
    \n POP R17 \
    \n POP R18 \
    \n POP R16 ");

    /* \n ADD R19,8 \
    \n CPI R19,64 \
    \n BRNE END \
    \n LDI R19,0 \
    \n END: STS line_r,R19 \
    */
    line_row+=8;
    if (line_row>=64){
    line_row=0;
    screen_row+=(unsigned int)0x26;
    }

    //------------- Pattern Draw ------------
    } else if( rasterline >= 0 && rasterline < 3 ) { // Duration First of Sequence Vertical Sync
    blank_sync;
    _delay_us(2);
    sync;
    _delay_us(30);
    blank_sync;
    _delay_us(2);
    sync;
    } else if(rasterline >= 3 && rasterline <= 5) { // Duration Second of Sequence Vertical Sync
    blank_sync;
    _delay_us(29);
    sync;
    _delay_us(2);
    blank_sync;
    _delay_us(29);
    sync;
    } else if( rasterline >= 6 && rasterline < 9) { // Duration third of Sequence Vertical Sync
    blank_sync;
    _delay_us(2);
    sync;
    _delay_us(30);
    blank_sync;
    _delay_us(2);
    sync;
    }/* else if( rasterline > 9 && rasterline <= 31) {
    } */else {
    blank_sync;
    _delay_us(4.5);
    sync;
    }

    rasterline++;

    if( rasterline > 262 ){
    screen_row=rasterline = 0;
    frame++;
    }

    }

    /* Main */

    int main() {
    DDRD=0b11111111;

    DDRB=0b11111011;
    PORTB=0xFF;

    DDRA |= 0xFF;
    PORTA |=0xFF;

    PORTC |=0xFF;
    PORTC =0xFF;

    timer_init();

    sei();
    while(1){
    if ((PINB & (1 <<PINB2))==0){
    Screen[270]=0x40;
    // here i want set delay
    Screen[270]=0x80;
    // here i want set delay
    }
    else
    Screen[270]=0x80;
    }
    for( ;; );
    }

    please help me...:arghh:
     
  2. riccardo

    riccardo Member

    Joined:
    Apr 15, 2010
    Messages:
    104
    Likes:
    2
    Location:
    United Kingdom
    Using delay should not create noise on your I/O pins. You should probably look at the circuit layout, PSU and ensure you have suitable decoupling capacitors, or maybe even a filter on the signal output.
     

Share This Page