Saturday, 15 December 2012

linked list operations sample code with functionalities (createList, display, delete by data, delete all, reverse list insertAt(int position, insertAtStart)

#include <stdio.h>
#include <stdlib.h>

struct node
{

    int info;
    struct node * link;
};

struct node * start=NULL;
void createList(int);
void display(void);
void insertAtStart(int data);
void insertAt(int position, int data);
void delByData(int data);
int del(void);
void reverseList(void);

int main()
{
    int choice=0, n, m, i, position;

    while(1)
    {

    printf("1.Create list\n");
    printf("2.Display\n");
    printf("3.Quit\n");
    printf("4.Insert at start\n");
    printf("5.Insert at position\n");
    printf("6.del by data\n");
    printf("7.del list\n");
    printf("8.Reverse list\n");

    scanf("%d", &choice);

    switch(choice)
    {
        case 1:
            system("clear");
  //          start = NULL;
            printf("How many nodes u want\n");
            scanf("%d", &n);
            for(i=0 ; i<n ; i++)
            {
                printf("enter element\n");
                scanf("%d", &m);
                createList(m);
            }
            break;
        case 2:
            system("clear");
            display();
            break;
        case 3:
            del();
            exit(0);
        case 4:
            system("clear");
            printf("enter element\n");
            scanf("%d", &m);
            insertAtStart(m);
            break;
        case 5:
            system("clear");
            printf("enter element position\n");
            scanf("%d", &position);
            printf("enter element data\n");
            scanf("%d", &m);
            insertAt(position, m);
            break;
        case 6:
            system("clear");
            printf("enter element data to delete\n");
            scanf("%d", &position);
            delByData(position);
            break;
        case 7:
            system("clear");
            if(del())
            {
                printf("successfully list deleted\n");
            }
            break;
        case 8:
            system("clear");
            reverseList();
            break;
        default:
            system("clear");
            printf("wrong choice\n");
            break;
    }
    }

    return 0;
}

void reverseList(void)
{
    struct node *p1, *p2, *p3;

    p1 = start;
    p2 = p1->link;
    p3 = p2->link;

    p1->link = NULL;
    p2->link = p1;

    while(p3 != NULL)
    {
        p1 = p2;
        p2 = p3;
        p3 = p3->link;
        p2->link = p1;
    }
    start = p2;
}

int del(void)
{
    struct node *tmp = start, *q;
    while(tmp != NULL)
    {
        q = tmp;
        tmp = tmp->link;
        free(q);
    }
    return 1;
}

void delByData(int data)
{
    struct node * tmp, *q;
    tmp = start;

    while(tmp->link != NULL)
    {
        if(tmp->link->info != data)
        {
            tmp  = tmp->link;
        }
        else
        {
            q = tmp->link;
            tmp->link = tmp->link->link;
            free(q);
            return;
        }
    }
}

void createList(int data)
{
    struct node *tmp, *q;
    tmp = malloc(sizeof(struct node));
    tmp->info=data;
    tmp->link=NULL;

    if(start==NULL)
    {
        start = tmp;
    }
    else
    {
        q = start;
        while(q->link!=NULL)
            q=q->link;
        q->link = tmp;
    }
}

void display(void)
{
    struct node *ptr=start;
    while(ptr != NULL)
    {
        printf("%d ", ptr->info);
        ptr = ptr->link;
    }
    printf("\n\n");
}

void insertAtStart(int data)
{
    struct node * tmp;
    tmp = malloc(sizeof(struct node));
    tmp->link = start;
    tmp->info = data;
    start = tmp;
}

void insertAt(int position, int data)
{
    struct node * tmp, * ptr = start;
    int count=1;

    while(ptr != NULL)
    {
        if(count != position)
        {
            ptr = ptr->link;
            count++;
        }
        else if(position > 1)
        {
            tmp = malloc(sizeof(struct node));
            tmp->link = ptr->link;
            ptr->link = tmp;
            tmp->info = data;
            return;
        }
        else if(position == 1)
        {
            tmp = malloc(sizeof(struct node));
            tmp->link = start;
            start = tmp;
        }
    }
    printf("no such position found..\n");
}

Friday, 24 August 2012

1ms TIMER0 led blinky(LPC21xx)




#include "LPC214x.H"
//CCLK=12xM=12x5=60MHZ
//PCLK=CCLK (VPBDIV=0x01)

#define LIMIT_1_MS 59999     //PCLK * X    = 1ms, ie X = 1ms/60mHz, ie X = 60000
void Timer_ISR(void) __irq;

unsigned int ms_count, flag = 0;
int main()
{
VPBDIV = 0X01;
//int timer0
T0TCR = 0x00;
T0TC = 0x0;
T0PC = 0x0; //The Prescale Counter is incremented on every PCLK. When it reaches the value
            //stored in the Prescale Register, the Timer Counter is incremented and
            //the Prescale Counter is reset on the next PCLK.
T0PR = 0x0;    //Prescale Register    set to zero
T0MR0 = LIMIT_1_MS;  //Match Register 0. MR0 can be enabled through the
                //MCR to reset the TC, stop both the TC and PC,
                //and/or generate an interrupt every time MR0 matches the TC.
T0MCR = 0x03;    //bit 0- Interrupt on MR0: an interrupt is generated when MR0 matches the value in the TC.
                //bit 1- Reset on MR0: the TC will be reset if MR0 matches it.

//interrupt
VICVectCntl11 = 0x00000024;
VICVectAddr11 = (unsigned int) Timer_ISR;
VICIntEnable = 0x00000010;

T0TCR = 0x01;

IO0DIR = 0x80000000;

    while(1)
    {
    }
}

void Timer_ISR(void) __irq
{
     if(ms_count == 1000)
    {
        if(flag == 0)
        {
            IO0CLR = 0x80000000;
            flag = 1;
            }
        else if(flag == 1)
        {
            IO0SET = 0x80000000;
            flag = 0;   
            }
        ms_count = 0;
    }

    ms_count++;
    T0IR = 0x01;
    VICVectAddr = 0x00;
}

Wednesday, 22 August 2012

Simple SPI0 - LPC21


led module can access using SPI0

Ref: BlueBoard_schematics_V1R1-3(LPC2148)


------------------------------------------------------------
#include <LPC214X.H>

void spiInit(void);
void spiSend(unsigned short);
void delay(void);

#define SPIF (1<<7)
#define DATA 0xFF


int main()
{
int i=1;
spiInit();

while(1) /*rotating led*/
{
spiSend(i);
delay();

i <<= 1;
if(i==0x0100)
i=1;
}
}

void spiInit()
{
PINSEL0=0x01500; /*MOSI0-bit 13:12 = 01, MISO0-bit 11:10 = 01, SCK0-bit 9:8 = 01 ,(SSEL0-not enabled, it is generated from code)*/ // ..,0001,0101,0000,0000 => 0x1500
IO0SET = 1<<7;   /* chip sel kept high*/
IO0DIR |= 1<<7; /* chip sel dir to output*/
S0SPCCR=0x8; /* SPI Clock Counter Register. This register controls the frequency of a master’s SCK0 */
S0SPCR=0x20; /*SPI Control Register - Device selected as master */
}

void spiSend(unsigned short data)
{
IOCLR0 = 1<<7; // enable slave select pin SSEL0
S0SPDR = data;
while (!(S0SPSR & SPIF)); /*SPI Status Register-SPI transfer complete flag. When 1, this bit indicates when a SPI
data transfer is complete */
IOSET0 = 1<<7; // disable slave select pin SSEL0
}

void delay(void)
{
int i, j;
  for(i=0; i<3000; i++)
  for(j=0; j<300; j++);
}
-----------------------------------------------------------------------------

External Interrupt - (LPC2148)

External Interrupt - BlueBoard_schematics_V1R1-3(LPC2148)

aim - when ext pin press buzzer rings



----------------------------------------------
eint2.h
----------------------------------------------


#ifndef __EINT2_H_
#define __EINT2_H_

#define EINT2_PINSEL PINSEL0
#define EINT2_PIN (1<<31)
#define EINT2_CHNL (1<<16)
#define LED_PIN (1<<25) //buzzer
#define LED_DIR IO1DIR
#define LED_CLR IO1CLR
#define LED_SET IO1SET

void initGPIO(void);
void initEINT2(void);
void beep(void);
void delay(void);
void lowBeep(void);
void lowDelay(void);

#endif


--------------------------------------------------
eint2.c
--------------------------------------------------

#include "Eint2.h"
#include <LPC214X.H>

void initGPIO(void)
{
LED_DIR |= LED_PIN; //setup buzzer
LED_SET = LED_PIN;
}

void ExtInt_ISR(void) __irq
{
EXTINT   = 0x00000004;
lowBeep();
VICVectAddr = 0x00;
}

void initEINT2(void)
{
EINT2_PINSEL |= EINT2_PIN;
VICDefVectAddr = (unsigned)ExtInt_ISR;
VICIntEnable |= EINT2_CHNL;
}

void beep()
{      
LED_CLR |= LED_PIN;
lowDelay();
LED_SET |= LED_PIN;
lowDelay();
}

void lowBeep()  //buzzer beeping
{      
LED_CLR |= LED_PIN;
lowDelay();
LED_SET |= LED_PIN;
lowDelay();
}

void delay()
{
int i=0, j=0;
for(i=0;i<3000;i++)
for(j=0;j<2000;j++);
}

void lowDelay()
{
int i=0, j=0;
for(i=0;i<3000;i++)
for(j=0;j<50;j++);
}


-------------------------------------------------------------------------
main.c
-------------------------------------------------------------------------

#include <LPC214X.H>
#include "Eint2.h"

main()
{
initGPIO();
initEINT2();

lowBeep();
while(1)
{
// beep();
}
}
-----------------------------------------------------------------------------

LPC2148- lcd interface sample code




---------------------------------------------------------------------------
lcd.c
------------------------------------------
#include <LPC214X.H>
#include "lcd.h"

void lowDelay()
{
int i=0,j=0;
for(i=0;i<3000;i++)
for(j=0;j<1000;j++);
}
void lowDelay1()
{
int i=0,j=0;
for(i=0;i<3000;i++)
for(j=0;j<10;j++);
}

void delay(int count)
{
  int j=0,i=0;
  for(j=0;j<count;j++)
  {
    /* At 60Mhz, the below loop introduces
    delay of 1000 us */
    for(i=0;i<350;i++);
  }
}

void lowBeep()
{      
IO1CLR |= (1<<25);
lowDelay1();
IO1SET |= (1<<25);
lowDelay();
}

void wait()
{
IO1CLR |= LCD_RS;
IO1SET |= LCD_RW|LCD_EN;
delay(1000);
while(IO0PIN & (1<<13));
IO1CLR |= LCD_EN;
}

void lcdWriteCmd(unsigned char cmd)
{
unsigned int tmp;
unsigned char mask = 0xf;
IO1CLR |= LCD_RS|LCD_RW;


delay(100);
tmp = (cmd>>4) & 0x0000000f;
IO0CLR |= mask<<10;
IO0SET |= tmp<<10;
IO1SET |= LCD_EN;
delay(10);
IO1CLR |= LCD_EN;
// wait();

delay(100);
tmp = (cmd) & 0x0000000f;
IO0CLR |= mask<<10;
IO0SET |= tmp<<10;
IO1SET |= LCD_EN;
delay(10);
IO1CLR |= LCD_EN;
// wait(); //
}

void lcdWriteDat(unsigned char cmd)
{
unsigned int tmp;
unsigned char mask = 0xf;
IO1SET |= LCD_RS;
IO1CLR |= LCD_RW;


delay(100);
tmp = (cmd>>4) & 0x0000000f;
IO0CLR |= mask<<10;
IO0SET |= tmp<<10;
IO1SET |= LCD_EN;
delay(10);
IO1CLR |= LCD_EN;
// wait();

delay(100);
tmp = (cmd) & 0x0000000f;
IO0CLR |= mask<<10;
IO0SET |= tmp<<10;
IO1SET |= LCD_EN;
delay(10);
IO1CLR |= LCD_EN;
// wait(); //
}

void strOut(char * data)
{
while(*data)
{
lcdWriteDat(* data);
data++;
}
}

void initLcd()
{
IO1DIR |= LCD_EN|LCD_RW|LCD_RS ;
IO0DIR |= D7_LCD|D6_LCD|D5_LCD|D4_LCD;

delay(1000);
  lcdWriteCmd(0x28);     /*   4-bit interface, two line, 5X7 dots.        */
  lcdWriteCmd(0x01);     /*   LCD clear                                   */
  lcdWriteCmd(0x02);     /*   cursor home                                 */
  lcdWriteCmd(0x06);     /*   cursor move direction                       */
  lcdWriteCmd(0x0C);    /*   display on      */
//   lcdWriteCmd(0x0F);
}
--------------------------------------------------------------------------------------------------
lcd.h
-----------------------------------------------------------------------
#ifndef __LCD__H__
#define __LCD__H__

#define LCD_EN (1<<22)
#define LCD_RW (1<<23)
#define LCD_RS (1<<24)
#define D7_LCD (1<<13)
#define D6_LCD (1<<12)
#define D5_LCD (1<<11)
#define D4_LCD (1<<10)


void initLcd(void);
void lcdWriteCmd(unsigned char cmd);
void lcdWriteDat(unsigned char cmd);
void strOut(char *);
void lowDelay(void);
void lowDelay1(void);
void delay(int count);
void wait(void);

void lowBeep(void);

#endif
------------------------------------------------------------------------------------------------------
main.c
---------------------------
#include "lcd.h"
#include <LPC214X.H>


main()
{
  
initLcd();  
delay(1000*10);
   
IO1DIR |= (1<<25);

lowBeep();
lcdWriteCmd(0x01);
delay(1000*10);
strOut("Hello LCD...");
delay(1000*100);
  while(1)
{
// delay(1000*6);
// lcdWriteCmd(0x10);
// lcdWriteDat(0x2d);
//
// delay(1000*6);
// lcdWriteCmd(0x10);
// lcdWriteDat(0x7c);
//
// delay(1000*6);
// lcdWriteCmd(0x10);
// lcdWriteDat(0x2f);
 
}
}
------------------------------------------------------------------------

Ubuntu lap screen-brightness-not-working..

==============================
========================

To get working brightness key. try following
execute sudo gedit /etc/default/grub
Change this line
GRUB_CMLINE_LINUX_DEFAULT="
quiet splash"
to something like below
GRUB_CMDLINE_LINUX_DEFAULT="
quiet splash acpi_osi=Linux acpi_backlight=vendor"
Execute sudo update-grub and reboot . See if brightness keys are working.
It is still possible ubuntu won't remember your brightness settings. So you have to change brightness each time.
Please let us know the result as many sony users are facing this problem.

This is for setting brightness manually after doing the above steps.
Try following for paths shown by ls /sys/class/backlight/*/
brightness and replace accordingly.
example paths will be like
/sys/class/backlight/acpi_
video0/brightness
  1. For the above path Get the maximum brightness:
cat /sys/class/backlight/acpi_
video0/max_brightness
Try a lower value to set the brightness, say output is 16 so I will try with half of it
echo 8 | sudo tee /sys/class/backlight/acpi_
video0/brightness
If this works, make this happen in each login automatically by doing the following
sudo gedit /etc/rc.local
Enter this line just before exit 0. It should look like
echo YOUR_VALUE_HERE > /sys/class/backlight/acpi_
video0/brightness exit 0 ==============================
============================

help link http://askubuntu.com/
questions/162317/screen-brightness-not-working

Recover GRUB after windos installation


First, open the Terminal.

Mount the partition you Ubuntu Installation is on. If you are not sure which it is, launch gparted (included in the Live CD) and find out. It is Usually a EXT4 Partition. Replace the XY with the drive letter, and partition number. Example: sudo mount /dev/sda5 /mnt. Then bind the directories, you need, like so:

sudo mount /dev/sdXY /mnt
sudo mount --bind /dev /mnt/dev
sudo mount --bind /proc /mnt/proc

Now we jump into that using chroot.

sudo chroot /mnt

Now install, check, and update grub. This time you only need to add the partition letter (usually a) to replace X. Example: grub-install /dev/sda, grub-install –recheck /dev/sda

grub-install /dev/sdX
grub-install --recheck /dev/sdX
update-grub

Now you can exit you mounted hard disk, and unmount.

exit
sudo umount /mnt/dev
sudo umount /mnt/proc
sudo umount /mnt

Shut down and turn your computer back on, and you will be met with the default Grub2 screen. You may want to update grub or re-install burg however you like it.