The finer details will be negotiated between the two sides over the summer.

Dairy UK, which represents processors, said it was pleased but a lot of work still needed to be done.

England's farming minister Jim Paice chaired a meeting between the NFU and Dairy UK at the event in Llanelwedd, Powys, which follows days of protests by farmers over the price they are paid for milk.

Today's announcement gave some hope for the long term but it did not solve the dairy farming issues of todayStephen James, NFU Cymru's deputy president

The NFU said the code of conduct stipulated that dairy farmers must receive at least 30 days' notice of a price change and retrospective price adjustments were no longer acceptable.

NFU Cymru's deputy president Stephen James, who farms in Pembrokeshire, said: "Today's announcement gave some hope for the long term but it did not solve the dairy farming issues of today.

"This agreement will give us the architecture we need to make sure that we don't end up with the same dysfunctional markets that are responsible for the dairy crisis we have today."

The NFU said the code also put in place conditions such as commitments to maintain prices within mutually agreed parameters.

Farmers will also have the right to withdraw from a contract with three months' notice if they disagree with a price change.

Further conditions include giving farmers the right to supply more than one processor.

Wales' Deputy Agriculture Minister Alun Davies said he had pushed for a Britain-wide and industry-led solution, to help ensure a level playing field across the domestic dairy market.

He added: "Good progress has been made today, and I am confident this step forward can help deliver real change within the sector."

Ceredigion Welsh Liberal Democrat MP Mark Williams said: "It is encouraging that an initial agreement has been reached voluntarily. Whilst there is still important work to be done on the detail, this is good progress and shows willingness from all sides to find a solution to the problems currently faced by dairy farmers".

Four processors plan to reduce what they pay for milk by 2p per litre on 1 August.

The Welsh government has said it could legislate to help those affected by price cuts if a UK-wide agreement was not reached.

Independent adjudicator

Before Monday's meeting, Mr Paice discussed the dairy issue with his Scottish and Welsh counterparts, Richard Lochhead and Alun Davies and suggested an independent adjudicator be created to oversee any voluntary code of conduct.

Farmers wanted a code of conduct that would give them more bargaining power over the price they can get for their milk.

They are often locked into fixed-term contracts with processing firms - effectively middlemen selling the milk on to customers including retailers and food production companies.

Morrisons, Asda and the Co-op have announced that from 1 August they would increase the premium they pay farmers for their milk.

Meanwhile, more than 100 dairy farmers and members of the Women's Institute protested at supermarket chain Asda's stand at the Royal Welsh Show on Monday.

NFU deputy president Meurig Raymond, who farms in Pembrokeshire, told Asda's head beef buyer Jim Biggars: "You can see the anger and despair among farmers here.

"My message to Asda is to step up to the plate. Please give these people a chance."

Mr Biggars said Asda had increased the premium paid to farmers by 2p.

Allan Edwards, director of public affairs for Asda, said it was committed to working with farmers to find a solution in the long term.

He said Asda increased the price it paid for milk last week, and other supermarkets then followed suit.

Processors say the global market milk price has fallen over the past year and that is why many farmers are being paid less.